Pages

Tuesday 19 May 2015

Immutable Nature of String

     Immutable means cannot be changed or altered. String instance once created are immutable. But still you can perform all type of operations like appending, reversing the string etc. Each time you perform any of these operations, a new string instance is formed and the original string on which operation is performed is unchanged.

      The below example program justifies the immutable nature of string:


public class MyStringImmutable
{
  public static void main(String[] args)
  {
       
        String str1="Coding"; // string literal.
       
        str1=str1.concat(" Unlimited");
       
        System.out.println(str1);
       
       

  }
}

Output:  

Coding Unlimited

      Now lets us see what happens when the above program is executed:

            String str1="Coding";

   After the execution of above line a new string instance “Coding” is formed and placed in string constant pool and variable str1 has the reference to the instance.

          str1=str1.concat("Unlimited");

      
      After the execution of above line the string instance “Coding” is concatenated with “Unlimited” to form a new string instance “Coding Unlimited” and it is also placed in string constant pool. The variable str1 now refers to “Coding Unlimited”.

What is the advantage of immutable nature of String?


   Suppose consider there are 3 reference variables, all referring to one object “Coding”. If anyone of the 3 reference variables tries to change the value of string object and if suppose string was mutable object then change will affect all three reference variable, this may not be desired in all circumstances. So to avoid such situation string objects are immutable in java.

Monday 18 May 2015

String Object Creation and Memory Management_String_2


Memory Management:

   Before dealing with String object creation, let us understand how memory is managed when handling String. The memory division is as shown in below image.


        




  String constant pool is a special memory which is used to store string objects.


String Object Creation:

  • By new keyword:


                   The new keyword will create a new String object and the object is placed in heap memory. Every time you use new keyword to create string object, each time JVM creates a new object and allocates memory.


     
         Given below is the code fragment creating a String object using new keyword. 


 String str1=new String("JAVA");






In the above code fragment a new string object is created and placed in heap memory, the string literal “JAVA” is placed in string constant pool and the variable ‘str’ refers to the memory location of string object.


  • By String Literal:



     The variable of type string is assigned directly with string literal using double quotes as shown below in the code fragment.



String str2="JAVA";



                  In the above code fragment the “JAVA” is String literal and str2 is variable of type string.


How String Literal way of string creation works?


       When you create a string literal like in the above code fragment, the JVM checks the string constant pool and scans for the match of string literal mentioned by you. If the string literal is already present in string constant pool then JVM simply returns the reference to the string literal already present in the string constant pool. If JVM doesn’t find any match for string literal mentioned then it creates a new string instance and places it in string constant pool.

Saturday 16 May 2015

String Handling in Java_1

What is String in Java?
  •      It is an object of type String.
  •           String is a sequence of characters.
  •      String is immutable.

Following are the different constructors provided by Java to create String.
  •      Create an empty String.

                 String str = new String ();
  •  Create a String by passing String object as constructor argument.

String str = new String (String strObj);
  •              Create a String initialized by an array of character.

String str = new String (char ch[]);
  •       Creating String by String Literal way.

String str = “JAVA”;

Example Program:

package com.string.handling;
// Package is container of similar classes and interfaces etc.

public class MyFirstString
{
   public static void main(String[] args)
   {
       String str1=new String(); // empty string object is created.
       
       // another way of string creation.
       char ch[] = {'J','A','V','A'};
       String str2=new String(ch); //String initialized by character array.
       
       //initializing with String object.
       String str3=new String(str2);
       
       //assigning one string to other.
       str1=str3;
       
       // creating string literal.
       String str4="JAVA";
       
       
       System.out.println("Empty String output: "+str1);
       System.out.println("String initialized by charater: "+str2);
       System.out.println("String initialized by String object: "+str3);
       System.out.println("String Literal: "+str4);
   }
}


Note: String is immutable. Once string is created you cannot change it.

There are two ways by which String can be created:

  •            By New Keyword.
  •            By String Literal.


// Stay tuned to explore difference between new keyword usage and String Literal.