Java Reference
In-Depth Information
... // This code would execute since s1 and s2
... // point to the same object.
}
The only way to modify the text associated with String s s1 or s2 is to point
these reference variables to different String objects. This is normally done by per-
forming some method that returns a String object and assigning the reference vari-
able to that String . String objects themselves are immutable. That means String
objects themselves cannot be changed; only the reference variables that point to a
String can be changed. For example, if you need another String value in s1 , you
must create a new String , and point s1 to it.
String s1 = new String ("Some text");
String s2 = new String ("Some other text");
// Assign s2 to a new String object.
// This object is the result of the trim() method, which removes leading
// and trailing spaces.
// String s1 contains no leading or trailing spaces, so the String object
// returned from trim() will contain the same text as String s1.
s2 = s1.trim();
if (s1.equals (s2){
// This code would execute since s1 and s2 contain the same text.
}
U NDERSTANDING R EFERENCE V ARIABLES WITH COBOL
It may be useful to revisit the COBOL example to help explain this concept. Recall
that the CALLER program defined two instances of MYSUB-CONTROL, as follows:
01 MYSUB1-CONTROL.
03 MYSUB1-ACTION-SWITCH PIC X.
88 MYSUB1-ACTION-INITIALIZE VALUE "I".
88 MYSUB1-ACTION-EVALUATE VALUE "E".
88 MYSUB1-ACTION-SET-AND-EVALUATE VALUE "S".
88 MYSUB1-ACTION-GET-CALL-COUNTER VALUE "G".
03 MSG-TEXT PIC X(20).
03 MSG-SIZE PIC 9(8).
03 MYSUB1-RETURNED-CALL-COUNTER PIC 9(10).
03 MYSUB1-PRIVATE-ITEMS PIC X(20).
Search WWH ::




Custom Search