12.9 String object by using new operator
Case 4:String s1 = new String("Java");
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhoGcIlCOlYPqflqoHi_7nwiFdLtfG5vDPScMuvPT0uUpjsdXDdkOffdttIPvMa60RSUJBvz3tClsuJCMnsDqAN73vb7-dlvPySJgGx6_ocvkfA5h1ZeRP47HyvvG-bwjOcsDUIFmmlA8jQ/s1600/new1.png)
- We are using new operator to create a String object, here JVM will create two objects, one is in heap memory which is assigned with reference and another object will be in SCP memory without reference.
- new : For s1 one object will be created and assign that object with s1 reference.
----------------------------------------------------------------------------
Case 5: String s1 = new String("Java");
String s2 = new String("Html");
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgG37_s2OxMa3wgYKOJelUpKCUNhbWEdUKSSVXQHDp6EgzDd2lN1ZWZQBqjxuF0_tivndBGwRL-WSZLofx6OTJ0TQaShM-_zh_COZ2eiTNTzPCcdRRODkJ0RetqcJ4j9zCqXhcqwOUCDX98/s1600/case+new2.png)
- We are using new operator to create a String object, here JVM will create two objects, one is in heap memory which is assigned with reference and another object will be in SCP memory without reference.
- new : For s1 one object will be created in Heap memory and assign that object with s1 reference, another object will be created in SCP memory without reference.
- new : For s2 one object will be created in Heap memory and assign that object with s2 reference, another object will be created in SCP memory without reference.
----------------------------------------------------------------------------
Case 6:
String s1 = new String("Java");
String s2 = new String("Html");
String s3 = new String("Java");
- We are using new operator to create a String object, here JVM will create two objects, one is in heap memory which is assigned with reference and another object will be in SCP memory without reference.
- new : For s1 one object will be created in Heap memory and assign that object with s1 reference, another object will be created in SCP memory without reference.
- new : For s2 one object will be created in Heap memory and assign that object with s2 reference, another object will be created in SCP memory without reference.
- new : For s3 one object will be created in Heap memory and assign that object with s3 reference, another object will not created in SCP memory because already object(Java) is available.
----------------------------------------------------------------------------
Thanks for your time.
Thanks for your time.
Nireekshan