• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How many number of string objects are created?

 
Ranch Hand
Posts: 173
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everybody,
How many number of strings are created??
I am going to give scjp 6.0 exam, how should i answer this...

Outpu: true
Is that only one object created or i should forget about string constant pool and say 4 objects??
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No you can't forget about the String constant pool, the answer would be 1 String object is created...
 
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Hareenda,

Please UseCodeTags while posting a code snippet.

BR,
 
Hareendra Reddy
Ranch Hand
Posts: 173
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Ankit ...

Hello prithvi..
now the code looks neater...
 
Prithvi Sehgal
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Hareendra,

Thanks for the prompt action. When are you appearing for the exam?

BR,
 
Hareendra Reddy
Ranch Hand
Posts: 173
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello prithvi,

Actually i am planning to give exam on march 9 and most probably i will schedule the exam this week....
and i have started preparation from dec 18,2010...
 
Ranch Hand
Posts: 92
PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know that we are never supposed to use == when comparing Strings. And im aware of the String litteral pool and how it works (at least I thought so), but can someone explain the following scenario to me.



How come s1 == s2 and s1 == s3 prints true while s1 == s4 prints false?
 
Prithvi Sehgal
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Malte,

The snippet


is actually evaluating to foobar which is already present in String Pool constant. So rather then creating a new object,
s3 is pointed towards the already present foo bar. Thats why s1 == s3 prints true.

IMO, thats the reasoning of the output.

BR,
 
Malte Wannerskog
Ranch Hand
Posts: 92
PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Prithvi Sehgal wrote:Hello Malte,

The snippet


is actually evaluating to foobar which is already present in String Pool constant. So rather then creating a new object,
s3 is pointed towards the already present foo bar. Thats why s1 == s3 prints true.

IMO, thats the reasoning of the output.

BR,



Yes, but why isnt it the same for s4? Shouldnt it also point to "foobar" which is already present in the Stringpool?
 
Prithvi Sehgal
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
IMO,

If you see the snippet it is like


This time as there nothing as foo in the String Pool constant, a new object will be created. In the next line you are just adding bar
into a newly created object. IMO, in this , already a new object was created thats why s4 is already pointing towards a new object.

HTH,
 
Malte Wannerskog
Ranch Hand
Posts: 92
PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Prithvi Sehgal wrote:IMO,

If you see the snippet it is like


This time as there nothing as foo in the String Pool constant, a new object will be created. In the next line you are just adding bar
into a newly created object. IMO, in this , already a new object was created thats why s4 is already pointing towards a new object.

HTH,



Im aware that there will be a new String litteral "foo" created in the pool.
But on line 5 when "bar" is added to s4... Will this result in a new String litteral "foobar" or will it point to the already existing "foobar"?

I guess my question is, can there be duplicates of String litterals in the pool?
 
Prithvi Sehgal
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No,

Duplicates are not allowed in String Pool constant as they are immutable they can be easily shared.
Now, its a trap for me too why both of them are not evaluating to true. I will get back, need to think
over it a little

BR,

 
Prithvi Sehgal
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

After thinking over it, as mentioned in K&B. For example


In our case s4 when this statement is executed


Java will create a new String object in non-pooled area which will refer to the literal foobar in
String pool. Remember that s1 and s4 are two different objects which are pointing to the same literal thats why
s1 == s4 is evaluating to false as both of them are two different String objects.

HTH,


 
Prithvi Sehgal
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

After searching through this, if you try to do something like this


Try this, now this time it will be true. It's a little strange behavior, but in our case s4 value was computed at runtime so it will return
false and additionally s1 and s4 are two different objects. Do check this

I will be waiting for another expert to walk in and comment over it.

BR,
 
Ranch Hand
Posts: 448
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Malte, you can also refer decompiled code to see what the String object references that you created are pointing to.....
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Malte,

I had the same doubt. Re:String Literals at compile time
Basically is something the compiler sees and checks if it is present in the String literal pool and equates to "foobar" but

In this case, at Line 2, the compiler does not know what the variable s4 holds and is equivalent to creating a new String with the value "foobar", which is different from the String literal value.
Hope this helps.
 
Malte Wannerskog
Ranch Hand
Posts: 92
PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Seema Kekre wrote:Hi Malte,

I had the same doubt. Re:String Literals at compile time
Basically is something the compiler sees and checks if it is present in the String literal pool and equates to "foobar" but

In this case, at Line 2, the compiler does not know what the variable s4 holds and is equivalent to creating a new String with the value "foobar", which is different from the String literal value.
Hope this helps.



Hmm, not really..
Are you saying that


is the same as

?

Because in K&B it says that


So how many objects are created when doing
?

Also, for the exam I guess it's enough to know that Strings are immutable and that we should compare using equals or compareTo?
Nevertheless I would like to know whats really going on .
 
Seema Kekre
Ranch Hand
Posts: 35
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is precisely what I am saying...

s4+="bar" -> s4= s4+"bar" -> s4=s4.concat("bar")

As per the Java Specifications for String.concat:
If the length of the argument string is 0, then this String object is returned. Otherwise, a new String object is created, representing a character sequence that is the concatenation of the character sequence represented by this String object and the character sequence represented by the argument string.

String s4="foo"; //Creates a string "foo" in the literal pool
s4+="bar";// Creates a new String "foobar" in the heap, s4 will now point to this new String.

Here is another good article Strings Literally

HTH.
 
Malte Wannerskog
Ranch Hand
Posts: 92
PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Seema Kekre wrote:
Here is another good article Strings Literally



Thanks!
 
reply
    Bookmark Topic Watch Topic
  • New Topic