Character Case in a String
String toUpperCase()
String toUpperCase(Locale locale)
String toLowerCase()
String toLowerCase(Locale locale)
Note that the original string is returned if none of the characters needs its case changed, but a new String object is returned if any of the characters need their case changed. These methods delegate the character-by-character case conversion to corresponding methods from the Character class.
These methods use the rules of the (default) locale (returned by the method Locale.getDefault(), §18.1, p. 1096), which embodies the idiosyncrasies of a specific geographical, political, or cultural region regarding number/date/ currency formats, character classification, alphabet (including case idiosyncrasies), and other localizations.
Examples of case in strings:
String strA = new String(“The Case was thrown out of Court”);
String strB = new String(“the case was thrown out of court”);
String strC = strA.toLowerCase(); // Case conversion => New String object:
// “the case was thrown out of court”
String strD = strB.toLowerCase(); // No case conversion => Same String object
String strE = strA.toUpperCase(); // Case conversion => New String object:
// “THE CASE WAS THROWN OUT OF COURT”
boolean test1 = strC == strA; // false
boolean test2 = strD == strB; // true
boolean test3 = strE == strA; // false
Concatenation of Strings
Concatenation of two strings results in a new string that consists of the characters of the first string followed by the characters of the second string. The overloaded operator + for string concatenation is discussed in §2.8, p. 63. In addition, the following method can be used to concatenate two strings:
String concat(String str)
The concat() method does not modify the String object on which it is invoked, as String objects are immutable. Instead, the concat() method returns a reference to a brand-new String object:
String billboard = “Just”;
billboard.concat(” lost in space.”); // (1) Returned reference value not stored.
System.out.println(billboard); // (2) “Just”
billboard = billboard.concat(” advertise”).concat(” here.”); // (3) Chaining.
System.out.println(billboard); // (4) “Just advertise here.”
At (1), the reference value of the String object returned by the method concat() is not stored. This String object becomes inaccessible after (1). We see that the reference billboard still denotes the string literal “Just” at (2).
At (3), two method calls to the concat() method are chained. The first call returns a reference value to a new String object, whose content is “Just advertise”. The second method call is invoked on this String object using the reference value that was returned in the first method call. The second call results in yet another new String object, whose content is “Just advertise here.” The reference value of this String object is assigned to the reference billboard. Because String objects are immutable, the creation of the temporary String object with the content “Just advertise” is inevitable at (3).
Some more examples of string concatenation follow:
String motto = new String(“Program once”); // (1)
motto += “, execute everywhere.”; // (2)
motto = motto.concat(” Don’t bet on it!”); // (3)
Note that a new String object is assigned to the reference motto each time in the assignments at (1), (2), and (3). The String object with the contents “Program once” becomes inaccessible after the assignment at (2). The String object with the contents “Program once, execute everywhere.” becomes inaccessible after (3). The reference motto denotes the String object with the following contents after execution of the assignment at (3):
Click here to view code image “Program once, execute everywhere. Don’t bet on it!”
Leave a Reply