Sunday, 18 September 2016

literal cases

12.7 Creating String object by using literal

Case 1
            String s1 = "Java";
                                        
  • If we use literal to create String object, here JVM will create only one object in SCP area and assign with reference.
So as per the Case 1, 
  • Literal : For s1 one object will be created in SCP area and assign that object with s1 reference. 
----------------------------------------------------------------------------
Case 2
             String s1 = "Java";
             String s2 = "Html";
                                   
So as per the Case 2, 
  • Literal : For s1 one object will be created in SCP area and assign that object with s1 reference. 
  • Literal : For s2 one object will be created in SCP area and assign that object with s2 reference.   
----------------------------------------------------------------------------
Case 3
             String s1 = "Java";
             String s2 = "Html";
             String s3 = "Java"
        
So as per the Case 3, 
  • Literal :For s1 one object will be created in SCP area and assign that object with s1 reference. 
  • Literal :For s2 one object will be created in SCP area and assign that object with s2 reference.
  • Literal :Here s3 will use s1 object which is available in SCP area instead of creating new object.
Note 
  • JVM before creating object in SCP, it will check if the same object is available in SCP area or not, if it is available it will reuse the existing object instead of creating new object, if object is not available then it will create.
----------------------------------------------------------------------------
Thanks for your time.
Nireekshan