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 |
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 Finally: the reference to the second called. It creates a second object is assigned to the object, containing all lower reference variable, d. case characters.
The temporary object
is used to construct a second object.
The reference to the second object is assigned to d
.
Now look at the last statement:
System.out.println( "Dark, forlorn...".toLowerCase() );
A String
is constructed.
Then a second String
is constructed
(by the toLowerCase()
String
is used a parameter for println()
.
Both String
objects are temporary.
Tedious review: