String s=new String("Test")
In this case 2 objects will be created one in the heap area and another is in scp (String constant Pool) and s
is always pointing to heap Object .
String s ="Test";
In this case only 1 Object will be created in scp and s
is always pointing to that object
Note
1.Object creation in scp is optional first it will check is there any Object already present in scp with required content if Object already present then existing Object will be reused if Object not already available then only a new Object will be created. But this only applicable for scp but not for the heap.
2.Garbage collector is not allowed to access SCP area hence even though Object doesnt contain reference variable it is eligible for GC if it is present in scp Area
All scp Objects will be destroyed automatically at the time of jvm shutdown.
Example 2;
String s1=new String("Test");
String s4=new String("Test");
String s2="Test";
String s3="Test";
Note:
Whenever we are using new operator compulsory a new object will be created in the heap area hence there may be a chance of existing 2 Objects with same content in heap area but not in scp, that is duplicate objects are possible in the heap area but not in scp.