Monday, 7 September 2009

JavaFX Week 2 - Oddities

I finished week two of the JavaPassion JavaFX course, but during the process I found some oddities that I wasn't expecting, so I've captured those details here...

Static types / Inferred types
It became obvious that with JavaFX the types can be inferred, or specified by the programmer.  As an experiment I tried to infer the type of a variable and then re-assign the value to some other value that just happened to be a different type, it turns out that's not allowed...

var x;         // Type not specified
x = "Rob";  // x becomes a String type

// But now we can't set it to an int
// because it's not compatible!
// x = 55;    

JavaScript would enable you to re-assign anything to the variable, because it's a dynamic language and although JavaFX looks kind of dynamic, it's not.  I guess that's because it uses Java under the hood and therefore it has to be statically typed. So then I thought, how could I make this 'feel' a little more like JavaScript.  My thoughts were to simply specify that it was an 'Object', after-all everything must extend object directly, or indirectly.

var x2:Object; // So will this work?
x2 = "Rob2";
x2 = 55;       // Yes

Whether this way of thinking is wrong, I don't know yet - it's too early to tell.

JavaFX data types can't be null
Okay, this one makes me feel really un-comfortable, why wouldn't you want the ability to make something null - All of these are illegal?

// null - For the JavaFX data types, you 

// can't set them to null!
// var b:Boolean = null; // Invalid
// var i:Integer = null; // Invalid
// var n:Number  = null; // Invalid

Now, the interesting bit here is that Booleans default value is false, Integer is 0 and Number is 0.0.  Are there any exceptions to this?  Well, it appears so...

var s:String  = null; // This compiles!

Oddly the above works - but why is it different between String and Boolean for example?  This made me think - 'I know, JavaFX can re-use existing Java libraries, so if I access a Boolean from a Java class that sets it to null, it must be null!'.

package week2blog;

public class JavaClass {

    public Boolean getMeANullBoolean() {
        return null;


var jc:JavaClass = new JavaClass();
var b2 = jc.getMeANullBoolean();
println("b2={b2} type:{b2.getClass()}");

// b2=null type:null

So, null is okay when integrating with existing Java code, so what can't I do it in JavaFX - I don't know yet, so I'll have to do some more digging!

My only thought was that perhaps JavaFX String is not the same as a Java String object? So one last experiment here:

println(b2.getClass());  // returns NULL!?
println(b2.TYPE);        // boolean

var myb:Boolean;
println(myb.getClass()); // returns java.lang.Boolean
println(myb.TYPE);       // boolean

Now that's odd isn't it! The JavaFX ones class is NULL, whereas the Java one is java.lang.Boolean... So this would imply that they are different?

I like the sequence stuff - that's pretty neat, but I couldn't help but think that I don't want to do things by the index of an object in the sequence, why couldn't I look it up, for example:

var days=["Mon","Tue","Wed","Thu","Fri","Sat","Sun"];
var weekdays = days[0..4];

// Would be nice to do something like this, but I haven't
// found anything yet:
// var weekdays2 = days[0..weekdays["Fri"]];
// var weekdays2 = days[0..weekdays.indexOf("Fri")];

Expressions / functions / returning null
I have to admit I am a little confused about when a null is expected to be returned from an expression or functions.  The documentation states

the JavaFX Script programming language is an expression language, which means that everything, including loops, conditionals, and even blocks are expressions. In some cases (such as while expressions) the expressions have Void type, which means they don't return a result value.

Expressions will return the value of the last line, but functions will return null / void if no return type specified.

So taking these a bit at a time...

If this is true 'If no return value is specified, a function returns Void by default', then why does the following return a number?

function test4() {
var x = 10;

println("test4 = {test4()}"); // this displays 10 still

If this is true 'In some cases (such as while expressions) the expressions have Void type' then I concur that this is true because this code snippet results in "Void type not allowed here".

//  In some cases (such as while expressions)

// the expressions have Void type

function test5() {
var x = 0;
x = 10;

// Can't do this "'void' type not allowed here"
// println("test5 = {test5()}");

If this is true 'but functions will return null / void if no return type specified' then why does this return a number!...

function test() {
return '9';
function test2() {
var x=10;

println("test = {test()}"); // returns 9 (as a String)
println("test2 = {test2()}"); // returns 10 (as a Number)

I can only get it to nag / not return a value if I say that the function doesn't return anything, by specifying a Void return (notice the capital 'V' by the way!).

function test3() : Void {
var x=10;

// Can't do this because test3() can't return anything

// println("test3 = {test3()}");  

I hope that I have given you some food for thought, if you happen to know any answers to those oddities, then please post a reply!

Thanks all,


  1. JavaFX have always been described as using strict typing, and the inference mechanism is quite limited, you often have to specify explicitely a type because of initialization cycle.

    I believe (but haven't studied the compiler...) that behind the scene, for performance reasons, a Boolean, Number or Integer are actually stored as boolean, float or int.
    That would explain why you cannot set them to null.
    I can be wrong... :-)

    "why does the following return a number"
    Because the last line is an expression...
    You can be bitten by this, see Beware of JavaFX's type inference!.

  2. TorNorbe from the JavaPosse attempted to leave a post here, but you can view it on their forums instead Thanks Tor!

  3. Russ Miles also had a problem posting his feedback, so here's a link to his Blog.
    Thanks Russ!

    Now I'm sure I'm meant to 'link' those blogs to this blog somehow, but I can't see the option!