In Java, String is derived data type. String is a class. String is not array of characters.String variables are Objects. The important feature of String is Immutability. Strings are immutable, which means contents of the string cannot be changed after they are initially constructed. There are some concepts people need to understand when using String objects. when we create strings by initializing the variables and the values are available at compile time, then java uses a technique called Interning to reduce the space used by duplicate strings.
For example
//Declare two Strings s1 and s2, Initialize them with the same value.
// so s1 and s2 should be two different objects of String Class But
// s1 and s2 both point to the same reference in the Heap, which means
// only one object will exist for "JAVA" and it is referred by s1 and s2.
// This is called automatic Interning of String
String s1 = "JAVA";
String s2 = "JAVA";
This can be proved by using the == operator. The equality operator(==) when used with primitive data types, checks whether the data or values are the same but when it is used with Objects, they check the equality of the Identity (whether they belong to the same instance) but not the equality of the state or data of the object.To check the equality of the data, we need to use the overrided equals() method of the object.
String s1 = "JAVA";
String s2 = "JAVA"; /* The equality operator (==) checks whether the String instance are equals, that is whether both s1 and s2 refer to the same memory instance. so in this case, it returns true as both point to the same reference */
if(s1==s2) System.out.println("Same Instance");
else System.out.println("Different Instance");
so you may have a question, if i change the content of the String through s1, will the change be visible from s2? There is the catch, as i told you before, String is Immutable, so the contents cannot be changed after the initial construction. so what happens when you assign a new value to the existing string.
String s1 = "JAVA";
String s2 = "JAVA";
s1 = "SUN";
In the above scenario, a new string object will be constructed with the value “SUN” and its reference is stored in s1.Similaryly, when we call any of the methods of the String class, a new String object is constructed and returned. Java does not perform interning,if the String values are determined at the RunTime. if you want to explicitly perform the interning, then we can call the intern method of the String.
char[] chars = {'A', ' ', 'S', 't', 'r', 'i', 'n', 'g'};
//the String org has a value available at compile time
String org = "A String";
//the value of aRuntimeString is not known till the run time, as it constructed from characters during execution String aRuntimeString = new String(chars)
//The equality operator will return false, because the aRuntimeString will a different instance even though it has the same value.
//The Interning of string didnt happen as the value was known at the Runtime
if(org == aRuntimeString) System.out.println("Same Instance");
else System.out.println("Different Instance");
//we can explicitly ask for interning by calling the intern method
// This method checks the intern table in the heap for the same value, if it exists then that reference is returned.
aRuntimeString = aRuntimeString.intern();
//Now both org and aRuntimeString will be referencing the same String object, since intern was done
// here the equality operator returns true
if(org == aRuntimeString) System.out.println("Same Instance");
else System.out.println("Different Instance");
If you want avoid interning when strings are created, then create string using the Constructor.
//In this Case, two different Object of Strings are Created.
String s1 = new String("NIIT");
String s2 = new String("NIIT");
//it will return false as both of them belong to different instances
if(s1==s2) System.out.println("Same Instance");
else System.out.println("Different Instance");
//equals methods checks equality of the contents of the String
if(s1.equals(s2)) System.out.println("String values are Same");
else System.out.println("String values are different");
References http://javatechniques.com/blog/string-equality-and-interning/
http://mindprod.com/jgloss/interned.html