+

Search Tips   |   Advanced Search

Data Representation

Java supports something called primitives, which are simple values like signed integers, decimal values and single bytes. Primitives do not provide any functionality, only non-complex data content. We can use them in computations and expressions, assign them to variables and pass them around in function calls. Java also provides a wealth of objects that not only carry data content (even highly complex data), but also provide intelligence in the form of object functions, also known as methods.

When you make the script call to task.logmsg( "Hello, World" ), you are calling the task object's logmsg() method.

Many Java primitives have corresponding objects. One example is integers, which can used in their primitive form (int) or by manipulating java.lang.Integer objects.

JavaScript does not employ the concept of primitives. Instead, all data is represented as JavaScript objects. Furthermore, JavaScript has only a handful of native objects as compared to Java's rich data vocabulary. So while Java differentiates between non-fractional numeric objects and their decimal counterparts – even distinguishing between signed and unsigned types, as well as offering similar objects for different levels of precision – JavaScript lumps all numeric values into a single object type called a Number.

As a result, we can get seemingly erroneous results when comparing numeric values in JavaScript:

if (myVal == 3) {
	// do something here if myVal equals 3
}
If myVal was set by an arithmetic operation, or references a Java decimal object, the object's value could be 3.00001 or 2.99999. Although this is very close to 3, it will not pass the above equivalency test. To avoid this particular problem, we can convert the operand to a Java Integer object to ensure a signed, non-fractional value. Then your Boolean expression will behave as expected.
if (java.lang.Integer( myVal ) == 3) { ...
Or we can make sure that your variables reference appropriate Java objects to begin with. In general, you will want to be conscious of the types of objects you are using.


Parent topic:

Java + Script ≠ JavaScript