Hi Shiv
It is about the same thing with any class.
For e.g.:
Integer i0 = new Integer(33);
Integer i1 = i0;
Integer i2 = i0;
Integer i3 = new Integer(33);
Now (i1 == i2) returns true, (i1 == i3) returns false.
The main difference for Strings is that identical constant strings are
recognized by the compiler as being the same String and stored only once.
For e.g.:
String s1 = "abc";
String s2 = "abc"; // The compilers "knows" it is the same
String s3 = new String("abc"); // The compiler understands that you
explicitly need another one.
Hope it helps
Mihai
Shiv Shankar a écrit :
Hi Guys,
Again I am here with new question. Have a discussion with my
friends and little bit confused about the string pool.
If I am creating a String object using
String obj1="shiv";
and
String obj2 = new String("shiv");
We know "shiv" will go in string pool. And when multiple objects
created like
String obj3 = "shiv";
String obj4 = "shiv";
then (obj3==obj4) and (obj1==obj4) will return true because they are
pointing to same object.
Now
If I created object
String obj5 = new String("shiv");
Here (obj2==obj5) will return false and (obj5==obj1) will return false.
It means both object obj2 and obj5 are different. But what about
string literal "shiv". Where it will reside in this case. Will it like
obj2 and obj5 are two different objects in Heap but internally they
are pointing to same object "shiv" on string pool.
Or
its like cloning of object with "shiv" string. And having two totally
different objects in Heap memory.
What about
String x = "shiv";
String y = new String(x);
String z = new String (y);
Here I think if I called a method on z object it will call same method
from y and then method of y will call same method of x and then
finally x will call method of String pool "shiv" object.
Correct me where I am wrong.
Thanks
Shiv Shankar
--
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/javaprogrammingwithpassion?hl=en