Wednesday, March 4, 2009

JavaFX for Java and JavaScript Programmers

JavaFX – big picture
• "scripting language" - but statically typed
• Type inferred rather than specified always
• "global" functions, variables in addition to classes and methods
• Multiple inheritance (may change to mixins!)
• Object Literals – create/initialize a new object
• Bind – simpler way to do observable/observer
• Sequences – flat, dynamic list with interesting operations
 
 
Comments
• Java
// this is single line comment
/* this is multi-line comment */
/** this is doc-comment */
• JavaScript
// this is single line comment
/* this is multi-line comment */
• JavaFX
• Nothing new! Same as Java!
 
 
Variables, Constants
• Java
String str = "hello"; // Type name = init_value
final double PI = 3.14; // "final" keyword => unmodifiable
• JavaScript
var str = "hello"; // "var" is a keyword
const PI = 3.14; // "const" is a keyword
• JavaFX
var str = "hello"; // "var" is a keyword
var str : String = "hello"; // optionally specify type
def PI = 3.14; // "def" keyword => unmodifiable
 
 
Primitive Types
• Java
• char, byte, short, int, long, float, double, boolean
• void (as return type)
• JavaScript
• Number, boolean, string, null, undefined
• JavaFX
• Value types – non-null default value, immutable types
• String, Number, Integer, Boolean, Duration
• Void (as return type)
 
 
String Literals
• Java
     • "This is a String"
• JavaScript
    • 'single-quoted string'
    • "double-quoted string"
• JavaFX
   • "double quoted like Java"
   • 'single quoted too works like JavaScript!'
   • "x is {x}" // string with embedded expressions
 
 
 
 Other Literals
• Java
• 22, 3.14, 2.41e10, 23.2e-2, 'c', true, false, null
• JavaScript
• Similar to Java. Adds undefined
• JavaFX
• Similar to Java
• Duration Literals (of type Duration)
2ms // 2 milliseconds
5s // 5 seconds
 
 
Operators
• The usual suspects: +, -, *, /, ++, -- etc.
• Reminder is "mod" and not % (unlike Java,
JavaScript)
• Usual comparisons: <, >, <=, >=, ==, !=
• No conditional operator. Instead there are more
expressions than statements. Use "if" expression
var v = if (x mod 2 == 0) "even" else "odd";
// instead of the Java, JavaScript expression
// (x % 2 == 0)? "even" : "odd"
• Logical (short-circuit) and, or are keyword
operators
// instead of the &&, || as in Java and JavaScript,
var inRange : Boolean = (x >= 0) and (x <= 100);
var done : Boolean = (timeRemaining <= 0ms) or
cancelled;
• Logical not is also a keyword operator
// instead of ! In Java, JavaScript,
var notInRange = not ((x >= 0) and (x <= 100));
 
• Assignment =, +=, -=, *=, /=
• Dynamic type check – instanceof
• Java, JavaFX – same "instanceof" operator
• JavaScript - "typeof" operator to differentiate between
primitives, object, function, array etc. And
"instanceof" to check against object types.
• Cast operation – not applicable to JavaScript
• Java: ArrayList al = (ArrayList) list;
• JavaFX: var al : ArrayList = list as ArrayList;
• No bitwise, no shift operators
 
• No String + (concatenation) operator as in Java
and JavaScript
• But, we can use embedded expressions in JavaFX
// instead of "hello, " + name, we use
var welcome = "hello, {str}"
• String expressions support format specifiers (like
java.util.Formatter)
• Support for localization