| 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
| method ofthe temporary object
Finally: the reference to the second is called.It creates a second
object is assigned to the object, with all lower
reference variable, d. case characters.
The temporary object
(using the shorthand constructor unique to Strings)
is used as a basis for 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 (using the shorthand constructor of Strings).
Then a second String is constructed (by the toLowerCase() method).
The second String is used a parameter for println().
Both String objects are temporary.