Pg 420 of K&B ,, says String s=new String("abc"); //creates two objects , and one refence variable I am not able to get how two oebjcts are created there . I can see only object being created Thnaks in advance
Originally posted by Kanchan Narang: Pg 420 of K&B ,, says String s=new String("abc"); //creates two objects , and one refence variable I am not able to get how two oebjcts are created there . I can see only object being created Thnaks in advance
The String literal "abc" is an instance of a String object. And you create a new String.
String works differently than other classes. When you have new String("abc"); and if "abc" is being created first time, jvm will allocate memory for both "abc" itself and the object that points to "abc". However if you do it twice:
new String("abc"); new String("abc");
You only have 3 objects created. I believe that one extra hidden object thing will not be tested on the exam.