Originally posted by Cindy Glass:
The variable first holds a reference to an object. Then it is used to create riddle. The riddle variable also gets a reference to the same object in line c. Setting first to null does not get rid of the reference in riddle, which is held until the end of the method.
String buffers are used by the compiler to implement the binary string concatenation operator . For example, the code:
x = "a" 4 "c"
is compiled to the equivalent of:
x = new StringBuffer().append("a").append(4).append("c").toString()
which creates a new string buffer (initially empty), appends the string representation of each operand to the string buffer in turn, and then converts the contents of the string buffer to a string. Overall, this avoids creating many temporary strings.