12.10 literal and new
Case 7:String s1 = "Java";
String s2 = new String("Java");
As per the Case 7,
- Literal : For s1 one object will be created in SCP area and assign that object with s1 reference.
- new : For s2 one object will be created in heap memory and assign that object with s2 reference, another object will not be created in SCP memory because already that object(Java) is available.
----------------------------------------------------------------------------
Case 8:
String s1 = "Java";
String s2 = new String("Java");
String s3 = "Java";
String s4 = new String("Java");
String s1 = "Java";
String s2 = new String("Java");
String s3 = "Java";
String s4 = new String("Java");
- Literal : For s1 one object will be created in SCP area and assign that object with s1 reference.
- new : For s2 one object will be created in heap memory and assign that object with s2 reference, another object will not be created in SCP memory because already that object(Java) is available.
- Literal : Here s3 will use s1 object which is available in SCP area instead of creating new object.
- new : For s4 one object will be created in heap memory and assign that object with s4 reference, another object will not be created in SCP memory because already that object(Java) is available.
----------------------------------------------------------------------------
Thanks for your time.
Thanks for your time.
Nireekshan