Friday 6 April 2012

Java Strings

String Concept

String is created in 2 ways in Java.

1. String as an object using new keyword
2. String as object using "" ie, double quotes

1. String as an object using new keyword:

        To represent  String as an object we use 'new' keyword as shown below.

           String s1 = new String("hello");
          
           Firstly we need to understand that String has separate memory location inside heap known as String Pool which is divided into 2 areas.
           1. String Constant Pool (SCP)
           2. String Non Constant Pool (SNCP)

           Now when we are creating a String object, an instance is created inside String Non Constant Pool area and the reference (s1) will be pointing to this location and also  1 more String object will be created inside String Constant Pool but the reference (s1) will not be pointing to this location. Both the memory areas has "hello" as String value.
                                                                  :
                                                                  :
s1  --------> string value  = "hello"              :           string value  = "hello"
                    String Non Constant Pool    :       String Constant Pool
                                                                  :                                        

2. String as object using "" ie, double quotes:

          Now when we are creating a String literal as shown below
         
          String s2 = "hello123";

          First the compiler will check is there any value with "hello123" in String Constant Pool, if there is no value then it will create a new value and s2 will point to that value, if there is a value matching then no new value will be created and s2 will point to that existing value only.

                                                             :
s1  --------> string value  = "hello"         :      string value  = "hello"
                                                             :       string value = "hello123"   <-------- s2
                                                             :
           String Non Constant Pool        :      String Constant Pool


         Now when we are creating 2 String literal with value = "hello" as shown below
         
          String s3 = "hello";
          String s4 = "hello";

          First the compiler will check is there any value with "hello" in String Constant Pool, as there is a value matching then no new value will be created and both s3, s4 will point to the existing value only.

                                                             :
s1  --------> string value  = "hello"         :      string value  = "hello"        <-------- s3, s4
                                                             :       string value = "hello123"   <-------- s2
                                                             :
            String Non Constant Pool       :      String Constant Pool


          Now when we are creating a new String object with value = "hello world" as shown below
         
          String s5 = new String("hello world");

          A new object will be created in Heap area and the reference (s5) will be pointing to this location, and also a String literal will be created in String Constant Pool as this value is not already existing in SCP.

                                                                :
s1  --------> string value  = "hello"            :      string value  = "hello"            <-------- s3, s4
s5  --------> string value  = "hello world"  :       string value = "hello123"      <-------- s2
                                                                :       string value = "hello world"   
                                                                :
            String Non Constant Pool               :      String Constant Pool


Analyze your String knowledge:

String s1 = "Hello World";
String s2 = "Hello World";

s1 == s2  -------> true (because both are pointing to same memory location)
s1.equals(s2) -------> true (string value is same)
String s3 = new String("Hello");
String s4 = new String("Hello");
String s5 = new String("Hello World");

s3 == s4  -------> false (because both are pointing to different memory location)
s1 == s5  -------> false (because both are pointing to different memory location)

s3.equals(s4) -------> true (string value is same)
s1.equals(s5) -------> true (string value is same)

String s6 = "Hello" + " World";
String s7 = "Hello";
String s8 = " World";
String s9 = s7 + s8;
String s10 = s7 + " World";

s1 == s6    -------> true (because both are pointing to same memory location)
s2 == s9    -------> false (because both are not pointing to same memory location)
s2 == s10  -------> false (because both are not pointing to same memory location)

s1.equals(s6)     -------> true (string value is same)
s2.equals(s9)     -------> true (string value is same)
s2.equals(s10)   -------> true (string value is same)

Important Note:
1. String Constant Pool will not contain any duplicate values.
2. From the above concept you can understand that String is Immutable i.e., as the value changes by any reference in SCP all other references which are pointing to that location will also get changed automatically which is known as Immutability.
3. == operator is used to check the memory location (address) of any 2 objects of same type or atleast sub class of other object which is used to compare, otherwise it throws compile time error.
4. equals() method is overriden in String class in order to compare the String values for any given 2 Strings.
5. String objects are immutable (ie., object is created as final implicitly) that is why when we are trying to change or modify the string value it will create a new object with same reference and then assigns new value to it.
Advantages:         High performance
Disadvantages:    Immutability

3 comments: