A good answer might be:

Line of code: OK Not Ok
String line = "The Sky was like a WaterDrop" ;  X  
String a = line.toLowerCase();  X  
String b = toLowerCase( line );    X
String c = toLowerCase( "IN THE SHADOW OF A THORN");    X
String d = "Clear, Tranquil, Beautiful".toLowerCase();  X  
System.out.println( "Dark, forlorn...".toLowerCase() );  X  

Temporary Objects

The "OK" answer for the last two lines might have surprised you, but those lines are correct (although perhaps not very sensible.) Here is why:

String d     =  "Clear, Tranquil, Beautiful".toLowerCase();
                 ---------------+-----------     ---+---
   |                            |                   |
   |                            |                   |
   |             First:  a temporary String         |
   |                     object is created          |
   |                     containing these           |
   |                     these characters.          |
   |                                                |
   |                                               Next: the toLowerCase()  method of
   |                                                     the temporary object is called.
Finally:  the reference to the second                    It creates a second object,
          object is assigned to the                      with all lower case characters.
          reference variable, d.

The temporary object (constructed with the shorthand constructor of Strings) is used as a basis for a second object. It is the second object who's reference is assigned to d.

In the last statement a String is constructed (using the shorthand constructor), then a second String is constructed (by the toLowerCase() method). The second String is used a parameter for println(). Both objects are temporary.

System.out.println( "Dark, forlorn...".toLowerCase() );

QUESTION 13:

  • Can an object reference variable exist that without referring to an object?
  • Can an object exist without an object reference variable referring to it?
Click Here after you have answered the question