Unlike Java, JavaFX does not overload the + operator to concatenate Strings:
import javafx.ui.*;
import javafx.ui.canvas.*;
Frame {
content: Label {
text: "Hello " + "World"
}
visible: true
}
Attempting to run the code above will result in the following console output:
compile thread: Thread[AWT-EventQueue-0,6,main]
compile 2.031
file:/C:/workspace/F3/HelloWorld.fx:6: incompatible types: expected Number, found String in "Hello "
file:/C:/workspace/F3/HelloWorld.fx:6: incompatible types: expected Number, found String in "World"
file:/C:/workspace/F3/HelloWorld.fx:6: incompatible types: expected String, found Number in text: "Hello " + "World"
Interpolation Work-Around[]
Interpolation, using the JavaFX String expression operators { }, provides a very readable work-around; the equivalent of Java's
String s = "Your score is " + n + " out of " + total + ".";
is this in JavaFX:
var s:String = "Your score is {n} out of {total}.";
concat() Method Work-Around[]
The concat() method may also be used in JavaFX to concatenate two Strings:
import javafx.ui.*;
import javafx.ui.canvas.*;
Frame {
content: Label {
text: "Hello ".concat("World")
}
visible: true
}