Java Reference
In-Depth Information
the println("z: {z}"); statement, while any change to x and y results in an
unconditional update.
x: 0 -> 1
y: 0 -> 2
Starting
Reading z
z: 0 -> 3
z: 3
Modify x
y: 2 -> 3
x: 1 -> 2
Reading z
z: 3 -> 4
z: 4
The performance ramifications of using lazy binding can be significant. Con-
sider the following code that includes a loop that is iterated over 10,000 times.
var v1 = 0;
var v2 = bind v1;
for(i in [1..10000]) {
v1 = i;
}
println(v2);
Because v2 is bound to v1 , it will be automatically updated for each of the
10,000 loop iterations. Because v2 is not referenced at all within the loop, those
recalculations represent a waste of CPU cycles. The developer might have been
better served using lazy binding and avoiding all those unnecessary updates.
With the advent of lazy binding, one might ask, why not just have all bindings be
lazy? The answer is that there will be occasions where you'll want to know about
a change in value when it actually happens. With lazy binding, as updates only
occur when the bound variable in question is accessed, this is not possible.
Chapter Summary
In this chapter, we looked at how binding can be used in JavaFX to associate
unrelated entities such that changes in one can be reflected in the other. To
achieve more clarity, this chapter deliberately focuses more on the language syn-
tax and deals less with the user interface aspects of the platform. Don't worry. As
we progress forward, you'll be able to apply what you've learned to solve more
realistic problems.
 
Search WWH ::




Custom Search