Using the 'new' keyword when instantiating a String object creates a
new object regardless of the contents of the String literal pool. The
new String object is not placed in the String literal pool (even
though the literal passed to the constructor may in fact become a
String literal pool entry).
You would generally only code the creation of String objects via the
'new' keyword if you needed to compare String object references
through the == operator for some reason, otherwise the use of a String
literal (e.g. "I'm a String!") is regarded as being more efficient.
On 28/06/2010, at 1:48 PM, Shiv Shankar Prajapati wrote:
Hi Amit,
I checked it that str2 and str5 are not equal. That is the thing
confusing. If we are creating using new keyword it will return
different objects created on heap. But pointing to same String in
string pool (According to book kathy sierra). That is the main
question if both are pointing to same Java object in String pool
then why we need this object in the heap. Is it due to OOP concept ??
On 6/28/10, Amit Ahire <[email protected]> wrote:
Hello Shiv,
First of all I would like to remind you of this particular thing
about String class : " A String class is the special and only class
where the object is instantiated without having to use the 'new'
keyword."
So if you are creating the instance
String str2 = new String("shiv");
or
String str2= "shiv";
Now coming back to what you are asking about, i dont understand why
do you say this,
If I created object
String obj5 = new String("shiv");
Here (obj2==obj5) will return false and (obj5==obj1) will return
false.
(obj2==obj5) and (obj5==obj1) will return true, wont it??
With regards,
Amit
On Sun, Jun 27, 2010 at 11:01 PM, Shiv Shankar <[email protected]
> wrote:
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
--
With Regards,
Shiv Shankar,
Persistent System Ltd.
http://sites.google.com/site/mcashivshankar/
--
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
--
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