Java Reference
In-Depth Information
JavaFX literals are also JavaFX objects, so it is possible to access their class's
respective methods directly from the literal. For example, the number literal 3.14
can be converted to an integer by calling its intValue() method inherited from
java.lang.Number .
var i:Integer = 3.14.intValue();
println(i);
Likewise, the integer literal 4 can be used to get its bit count by invoking the
bitCount() method inherited from java.lang.Integer .
println(4.bitCount(1));
println("Now is the time".substring(0,3));
When the basic JavaFX types are passed as parameters to Java methods or a Java
type is being assigned to one of the JavaFX basic types, an attempt is made to
convert between the two. The following eight tables, Tables 11.2 through 11.9,
illustrate the conversion rules for each of the JavaFX basic types, including
sequences. The first column is the JavaFX type, the second column is the Java
type, the third column is the rule when the JavaFX type is passed as a parameter
to a Java method call. The last column is the rule when a Java type is being
assigned to a JavaFX type, either directly or as a result of a return type from a
function or Java method.
For parameter conversion, if an automatic conversion is not supported, an alter-
native is presented in the Parameter Conversion column. Usually, the type needs
to be converted to the Java Type using one of the xxxValue() methods on the
JavaFX object. For example, if the JavaFX type is a Number , when trying to con-
vert to the Java java.lang.Double class in the Java method parameter, you need
to invoke the doubleValue() function on the JavaFX Number class. This listing
illustrates this.
var aNumber = 3.14;
aJavaObject.callDoubleMethod( aNumber.doubleValue() );
In the Assignment column, the message, “possible loss of precision” means the
compiler will issue a warning message that the conversion will lose some accu-
racy. If you want to avoid this warning message, the return type should be nar-
rowed to the JavaFX type. For example, when converting a Java double to a
JavaFX Long, use the longValue() function on the returned type. For example:
var aLong : Long =
javaObject.callJavaMethodReturn_double().longValue();
Search WWH ::




Custom Search