• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

String literal confusion...

 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String x = new String("abc");
String x1 = new String("abc");

I understand 3 objects will be created .
(one is x and two idential objects---> new String("abc")( SAY IDENTICAL OBJ1 & IDENTICAL OBJ2)

Please correct me if I amm wrong.......

First obj x will be created in the heap and a reference (i name the reference as x) in the pool, pointing to its value which is another object ( new String("abc"))

while creating x1 compiler will see that already a reference to new String("abc") exits and thus will not create another object.

Now Im confused..............
I guess one more reference be created (say x1)??? and it will point to the object where x points? ie IDENTICAL OBJ1

But then IDENTICAL OBJ2 (String x1 = new String("abc") will also be created.

When I want the value of x1 ( say later in the program ) which object will it point in the heap ??? IDENTICAL OBJ1 OR IDENTICAL OBJ2 ( both are new String("abc"))

DO I MAKE SENSE???

Today I have finally decided to give SCJP (Seriously) and ordered my K&B coz I work in Java Platform but so many basic concepts are not clear

Thanks
Mou
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Allocation and deallocation of String literals is not on the SCJP exam. That said, you can find a ton of information regarding it by searching this forum.
 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String x = new String("abc");
String x1 = new String("abc");

The above code will create 2 String objects, since you explicitly use the new operator.

However, if the code looked like this:

String x = "abc";
String x1 = "abc";

then only 1 String object will exist, which is the interned String literal "abc".
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Tybon Wu:
String x = new String("abc");
String x1 = new String("abc");

The above code will create 2 String objects, since you explicitly use the new operator.



The above code actually creates 3 String objects. The first object is "abc" and this is put in the String pool. The second and third objects are referred to by x and x1, with contents, but not references, equal to "abc"


 
mou haj
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Fletcher,

I understand it now... What I wrote initially was totally wrong..
while loading the program "abc" is created for the line
String X = new String ("abc"); ( with a reference in the pool)

and while loading
String X1 = new String ("abc"); no more object is created as there is already a reference of a same object in the pool

and while execution X and X1 will be created and both will be reffered ( or assigned) to the reference in the pool (ie to abc)
 
Fletcher Estes
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome, just one thing:

Originally posted by mou haj:
and while execution X and X1 will be created and both will be reffered ( or assigned) to the reference in the pool (ie to abc)


X and X1 will not be assigned a reference to the "xyz" in the String pool, they will be assigned a reference to completely new String objects. It's just that they will have the same content. Whenever you use the new operator you always get a new object and a new reference.

To re-use the "abc" reference from the String pool, you just assign it directly like: String X2 = "abc"; By doing that, you ensure that you use the existing object (if it exists) rather than creating another one.

Another way to get references from the String pool is to use the intern() method like: String X3 = X1.intern() This forces the JVM to examine the String pool for any String with the same contents as X1, and if it finds one, it returns a reference to it.

It's a little confusing...
 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So will be right if I say...

As there are 3 objects created...

When the first statement String x = new String( "abc" ); is compiled an "object" with "abc" as its content is created in the pool.

When the other String x1 = new String( "abc" ) is compiled it will try to create another object with "abc", but since there already exists one it will not create an duplicate.

But when the above 2 are executed. They will create 2 other different objects in their program space respectively(since new is used) and x, x1 act as references to these two objects.

Here is where i think i am missing, what will happen to the string pool object since x , x1 have there own objects to which they refer and have their own contents.

if it were
String x = "abc";
String x1 = "abc";

Then no new objects is created and x , x1 will refer(point) to the object in the pool with contents "abc". Right?

Thanks,
Kits
 
Tybon Wu
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Fletcher Estes:


The above code actually creates 3 String objects. The first object is "abc" and this is put in the String pool. The second and third objects are referred to by x and x1, with contents, but not references, equal to "abc"



Yes that's what I meant. Forgot that there was a string literal in the statement too
 
Fletcher Estes
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Here is where i think i am missing, what will happen to the string pool object since x , x1 have there own objects to which they refer and have their own contents.

if it were
String x = "abc";
String x1 = "abc";

Then no new objects is created and x , x1 will refer(point) to the object in the pool with contents "abc". Right?


You've got it about right now. We can simplify it by looking at just one line of code:
What's happened here?

1. The String "abc" is now in the String pool.
2. Another String s is now on the heap. The contents of s are "abc", but it is in no way related to the String pool.

So what happens to the "abc" in the String pool now that s is pointing to a different object? The JVM leaves it in the String pool just in case it is used anywhere else in the program. This is a form of optimization to prevent the JVM having to recreate a new object that is identical to one that has been used already.
 
Kitty Dayal
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Fletcher,

What about this? How many are created(objects) and where?

String a = new String( "abc" );
String b = a;
String c;
c = a + b;

This is from Bill's book, in which he say's 2 objects. One with the 'a' and the other with 'c'. He does not say anything about the object created in the pool. Should we count the object created in the pool? Obviously c refers a different object.

Thanks,
Kits
 
Tybon Wu
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Kitty Dayal:
Hi Fletcher,

What about this? How many are created(objects) and where?

String a = new String( "abc" );
String b = a;
String c;
c = a + b;

This is from Bill's book, in which he say's 2 objects. One with the 'a' and the other with 'c'. He does not say anything about the object created in the pool. Should we count the object created in the pool? Obviously c refers a different object.

Thanks,
Kits



A String may or may not be created in the pool, depending on whether the pool already contains a String with the same value. Given the code you gave without any assumptions, you can only say for certain that 2 new String objects will be created.
 
You ought to ventilate your mind and let the cobwebs out of it. Use this cup to catch the tiny ads:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic