Wednesday, December 28, 2011

Java String Class

Hi guys....This is my first ever post. Hope this would be useful.
            So the language Java has lot of classes. And you can know about them if you visit to the Java API (http://docs.oracle.com/javase/1.5.0/docs/api/) . So if you follow through to the lang package in the API ,you will be able to find the java String class. But sometimes i find them little bit harder to understand.
            Java is always dealing with objects. So we have to get familiar with objects if we are to deal with any functionality in Java. This link will help you with classes and objects if you are new to java .(http://www.javacoffeebreak.com/java102/java102.html)       
             String objects are immutable. That means the values cannot be changed after they are created.
Let's say
                         String str; //declaring a variable
                         str=new String("ABC");
                         str.toLowerCase();
                         system.out.println(str);

Though we aspect to print "abc" it will still print "ABC" .Because we have created only one instance and we have given a value ABC for it. Because of this immutability we cannot change the original value .When we say str.toLowerCase ,what has happen there is a new String object is created and given the value "abc" .And the reference of that object is returned by toLowerCase() method.In that case if we say,
                        s=s.toLowerCase();
then it will print "abc". Because the new reference is saved to s reference.So now s will talk about the changed object.If you want to learn more about immutable objects ,you can follow this link. http://www.javapractices.com/topic/TopicAction.do?Id=29
And also String class is final one.Thus it can't be extended.

There are two ways of creating a string object .
                 - Using the new operator
                                 eg :- String str=new String("abc");
                 - Using a String literal
                                 eg :- String str="abc";

What is the difference between these string creations.
                 for that we can use the equals method and the == operator. Equals method is always compare the contents rather than the two objects references.
Let's see the below example,

class Demo{
public static void main (String args[]){
       String s1=new String("abc");
       String s2=new String("abc");

       String s3="abc";
       String s4="abc";

       System.out.println("s1==s2 : "+(s1==s2)); //prints false

               System.out.println("s1.equals(s2) : "+(s1.equals(s2))); //prints true 
       System.out.println("s3==s4 : "+(s3==s4));//prints true
               System.out.println("s3.equals(s4) : "+(s3.equals(s4))); //prints true
       System.out.println("s1==s3 : "+(s1==s3));//prints false
}
}

When create a String using new keyword , it will create a new object with the  reference s1 (as for the first statement) n will create another object with the reference s2 (as for the second statement).So although the contents are same the references are different. But when creating a String without using the new keyword it will be created in the String pool.This is a one tricky way the JVM uses to increase performance and decrease memory over head.
        
Because creating objects all the time is wasting memory so when creating without the new keyword first the JVM checks the String literal pool.If the string already exists in the pool then the reference of that instance returned but if does not exists then a new String object instantiates and is placed in the pool.
        
So in the above example s3 and s4 references are same because String contents are same and both are String literals.   

Unfortunately when creating using new key word it will create a new String object out of the pool even though there is exactly a same String in the pool. 

These are the basics that you should know about Java String class .