Language Fundamentals - String Literals
String literals are enclosed in double quotes "This is a string literal."
A string constant expression occurs when two or more string literals are concatenated "This is " + "a string " + "constant expression."
Character escape codes can be used in String literals "A line with a carriage return /r"
!!! Warning !!!
You cannot use the character literals /u000a (newline) or /u000d (carriage return) in String literals as they will be interpreted as LineTerminators , not as input characters (JLS §3.10.5) "A line with unicode carriage return character /u000d"
If you use octal values in Strings to represent characters be sure to use a zero prefix (JPL pg33)
Note: the zero prefix is not required for octal values in char literals "/0116" octal value equivalent to escape
char /t followed by 6 "/t6"
"/116" interpreted as letter N
Each String literal is a reference to an object of class String.
String literals or strings that are the values of constant expressions, are interned so as to share unique instances.
public String.intern() (JSK 1.3)
"Returns a canonical representation for the string object.
A pool of strings, initially empty, is maintained privately by the class String.
When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the equals(Object) method, then the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned.
It follows that for any two strings s and t, s.intern() == t.intern() is true if and only if s.equals(t) is true.
All literal strings and string-valued constant expressions are interned."
Output from (JLS § 3.10.5) example code:
the JLS gives example code using literals in the following classes:
class test
class Other (in the same java file as class test)
class other.Other (in a different package)
the code gives the following output:
String variables initialized as:
String hello = "Hello"
String lo = "lo"
(1) hello == "Hello" true
(2) Other.hello == hello true
(3) other.Other.hello == hello true
(4) hello == ("Hel"+"lo") true
(5) hello == ("Hel"+lo).intern() true
(6) hello == ("Hel" + lo) false
literal strings will represent the same reference if they are created
in the same class and in the same package
in different classes within the same package
in different classes in different packages
using constant expressions computed at compile time
by explicitly using the intern() method and the resulting string is already in the string pool
literal strings will represent different references if they are newly created at runtime (Line 6)
Summary
if String objects having the same data are created using a constant expression , a string literal , a reference to an existing string, or by explicitly using the intern() method, their references will be the same
if String objects having the same data are created explicitly with the new operator or their values are computed at runtime, their references will be different
String str1 = "Lions and Tigers and Bears!";
String str2 = "Lions and Tigers and Bears!";
String str3 = str2;
String str4 = new String("Lions and Tigers and Bears!");
String str5 = " Oh my!";
String str6 = "Lions and Tigers and Bears! Oh my!";
String str7 = str1 + str5;
String str8 = (str1 +" Oh my!").intern();
Comparison output:
str1 == str2 -> true // the str2 literal existed ("interned")
str1 == str3 -> true // hold the same reference
str1 == str4 -> false // str4 explicitly created
str2 == str3 -> true // hold the same reference
str2 == str4 -> false // str4 explicitly created
str3 == str4 -> false // str4 explicitly created
str6 == str7 -> false // str7 computed at runtime
str6 == str8 -> true // explicit use of intern() at runtime
JSK 1.3 for the java.lang.String class states:
"Strings are constant; their values cannot be changed after they are created. String buffers support mutable strings. Because String objects are immutable they can be shared."
In other words, because the compiler knows the strings original value cannot be changed once it's created it can safely use existing data and avoid cluttering up memory with duplicates.
Traps
using == operator to compare contents of two string reference variables pointing to different String objects