STRING CLASSES

String Classes


  • In java we are using alphabets they came up with character type but by using char data type we can manipulate only one character at a time.

  • If we want to use or work with multiple characters at a time we can go for character Array (i.e. char []) but by using character Array we can work with multiple characters but manipulation of multiple characters is very difficult.

  • To overcome these drawbacks Sun People developed one class it is named as String. It is available under java.lang.string package. This package will come automatically whenever we use this string class without explicitly declaration.

  • String class is a final class so we can’t make any other class as a subclass to string class. String class cannot support inheritance concept why because it is a final class, it can’t inherit to subclass.

  • For string class we can able to create objects in two ways.

      1.) By using pure literal values.
      2.) By using ‘new’ operator.

  • String class objects are immutable. It means once if we create the string objects we can’t able to modify that objects in the same location.

  • If we try to modify existing objects the result of modification value will be created as a new object that new object is created to corresponding reference variable and the previous object(i.e. original object) will be abanded. There is no live link from stack to heap then it is eligible for Garbage collector.

  • Two ways of object creation.

      1.) By using pure’ literals’.
         Eg:Integer i=25;
         String s=”abc”;
      2.) By using ‘new’operator.
         Eg:Integer i=new Integer(25)
         String s=new String(“abc”);

  • The two ways of object creation is only for wrapper classes and string classes.

  • The string type objects have two types of memory location in heap memory.

      1.) String constant pool.
      2.) String Non-constant pool.

  • If we create any string type object by using literal that object will be stored under string constant pool.

  • In heap memory the object can be divided as

    javapadho
    Heap memory by manage by String class

By using pure’ literals’


  • String constant pool won’t allows duplicate objects.

  • Let us take one example with pure literal values i.e
     String s1=”abc”;
     String s2=”123”;
     String s3=”abc123”;
     String s1=”abc123

    javapadho
    Heap memory by manage by String class

  • If we try to create new object and assign with the same reference variable(i.e. s1) it will point to the existing object only.The existing object won’t modify it will create new object and assign the same reference variable.

  • In the above example s1=”abc123” it assigns to previous object with same reference variable and previous object i.e. s1=”abc’ will be abanded then It is eligible for garbage collector.

    javapadho
    Heap memory by manage by String class

  • Once we create string object and if we try to modify that object it is not possible to modify string object in the same location why because it is immutable.

  • Then new object will be created with live link cut from the previous object and it is points to modified object.

  • The example with literal and live link.

Example 1:

 
							
                      public class LiteralValues 
                      {
                         public static void main(String[] args) 
                         {
                            String s1  = “ja”;
                            String s2 = “va”;
                            String s3 = “java”;
                            String s4 = s1 + s2;
                            String s5 = “ja” +”va”;
                            String s6 = s1 + “va”;
                            String s7 = “ja” + s2;
                            System.out.println(s3);
                            System.out.println(s4);
                            System.out.println(s5);
                            System.out.println(s6);
                            System.out.println(s7);
                         }
                      }                            

   Output:

java java java java java

Explanation:

    javapadho
    Heap memory by manage by String class

  • We can create object using string class two way

    i)String s1 = “abc” and String s1 = new String(“abc”);
    ii)Only case of wrapper classes and String we can create object without new operator.
    iii)If we create string object using literal than this store in string constant pools.
    iv)If we create String object using literal and + operator with permanent reference than object create inside Non Constant Pool. In non constant pool object present than create new location inside NCP.
    v)If we create String object using literal and + operator with permanent reference than object create inside Non Constant Pool. In non constant pool object present than create new location inside NCP.

  • System.out.println(s3==s4); fasle
    System.out.println(s3==s5); false
    System.out.println(s4==s5); false
    System.out.println(s4==s6); false
    System.out.println(s3.equals(s4)); true
    System.out.println(s3.equals(s5)); true
    System.out.println(s3.equals(s6)); true
    System.out.println(s3.equals(s7)); true
    System.out.println(s3.hashCode()); Same int value because content is same
    System.out.println(s4.hashCode());

  • In java sun people overloaded ‘+’ operator inside string class for concatenating two string objects.
      Eg:   int i=5+10;   i.e. i =15
         String s=”abc”+”xyz”;   i.e. s=abcxyz

  • In string class ‘+’ operator will append second parameter value at the end of first parameter value.

  • While string concatenation by using ‘+’ operator if both are literal objects then the result will be created in constant pool.
      Eg:   s7

  • If there is any live link or reference variable while concatenation then the result will be created under Non constant pool.
      Eg:   s4 ,s6 ,s7

  • In the above s7 object it has one live reference(i.e. s2) and one literal object(i.e.”ja”) then the result will be under Non constant pool.

  • In string Non constant pool will allows duplicate objects also.

By using ‘new’operator


  • For creating the string object by using ‘new’ operator

Example 2:

 
                        
                   public class NewOperator
                   {
                      public static void main(String[] args) 
                      {
                         String s1=new String("sdj");
                         String s2=new String("info");
                         String s3=new String("soft");
                         String s4=new String("java");
                         String s5=s1+"java";        
                         String s6="sdjinfo"+s2;
                         String s7=s1+s2;
                         String s8="sdj@"+"java";
                         System.out.println(s1);
                         System.out.println(s2);
                         System.out.println(s3);
                         System.out.println(s4);
                         System.out.println(s5);
                         System.out.println(s6);
                         System.out.println(s7);
                         System.out.println(s8);
                      }
                   }                        
    javapadho
    Heap memory by manage by String class
  • The above string objects will be created in heap memory.

  • If we create any string object by using new operator that object will be created in Non-constant pool.