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

String objects creation

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was referring http://valiveru.tripod.com/java/jvaltest.html.

How many String objects are created when we run the following code.

String s1,s2,s3,s4;
s1 = "Hello";
s2 = s1;
s3 = s2 + "Pal";
s4 = s3;

I though following will be the new objects
"Hello"
"Pal"
"Hello"+"Pal"

I answered 3. Exam says that is wrong? Can some one guide me what is the correct answer. Thanks in advance
 
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
I think only 2 objects will be created.
1. "Hello" (s1 and s2 point to this)
2. "Hello Pal" (s3 and s4 point to this one)

Since "Pal" is not being assigned to any reference, it is just a compile time constant.

Abhishek
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the no. of objects created is 3. Even though there is no reference to String constants, they will be added to the String pool once it is used.
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think Abhishek is right in saying that only 2 objects are created for the given example.

I just checked the hashcodes of the strings by adding the below to the provided code.

System.out.println(s1.hashCode());
System.out.println(s2.hashCode());
System.out.println(s3.hashCode());
System.out.println(s4.hashCode());

Which returned the following values when run on my machine
69609650
69609650
-728040823
-728040823

So there will be 2 objects created in all...
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Remember that hashcode is not guaranteed to be different for unequal objects, also two different String instances with the same value will return the same hashcode. Also, even though there is no reference to the String literal (Object) "Pal", it is still an object and will be added to the String pool (assuming "Pal" doesn't already reside in the string pool).
 
Abhishek Asthana
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kristian is right about hashcode theory. In Java, the hashcode produced for any string is calculated by adding the hashvalues for all characters. Hence in this case the hashcodes for s1&s2 and s3&s4 match.
But why should "Pal" create a new object?
 
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

Total three objects will be created
1- "Hello"
2- "Pal"
3- "HelloPall"
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[Abhishek]: In Java, the hashcode produced for any string is calculated by adding the hashvalues for all characters.

Not true - the position of each character is also relevant. If it were a simple sum of hashcodes of characters, then *"ab".hashCode() == "ba".hashcode()) would evaluate to true. It doesn't.

Hence in this case the hashcodes for s1&s2 and s3&s4 match.

No - that's because in fact, s1 and s2 refer to the same object. And s3 and s4 refer to the same object. Because the code explicitly assigned s2 = s1, and s4 = s3. It's not just some coincidence.
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No - that's because in fact, s1 and s2 refer to the same object. And s3 and s4 refer to the same object. Because the code explicitly assigned s2 = s1, and s4 = s3. It's not just some coincidence.

I don't think Jim Yingst is right, when I create a string object
String s = "Hello", It won't create a new object in the pool for me if there is already a "Hello" in the pool, it will just set the reference of my variable s to the exisiting "Hello".

I believe String objects are not garbage collected like normal Objects. They still exist in the pool even if there is no variable referencing it.
 
Ranch Hand
Posts: 1847
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jim is right, but he doesn't qualify his statement enough.
For String (which is what we're talking about here) he is right.
For some other class he may not be (as each class can and often will have a distinct hascode algorithm relevant to the data contained within an instance of that class).

You are correct in assuming that garbage collection for Strings in the String constant pool is different from garbage collection of any object anywhere else, but that's not relevant in this scenario as the question was how many objects are created, not how many exist at the end of the code block (the answer to that could be undefined even).

In this scenario, at most 3 objects are created on the string constant pool.
A total of 4 references are created, 2 each to 1 of these objects.
The last of them ("Pal") has no reference at all but does exist and would be reused if another assignment 's5 = "Pal";' were added.
I had to qualify my statement with "at most" because as Kristian notes any one or more of the strings may already have existed in the string constant pool and therefore might not have been created at all, but during the exam you can (almost, it will be explicitly mentioned if not) assume that such situations will not occur (in fact, because of tricks like this you're highly unlikely to see questions like this using String as a datatype on the real exam).
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Three objects will be created as mentioned by Amir
1- "Hello"
2- "Pal"
3- "HelloPall"

As the String objects are immutable, we can not modify the object after creating. Even though we try to modify the object, a new object will be created with out modifying the earlier object.
 
Nilesh Deshpande
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks all.
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sujit, my statements said nothing at all about the string pool, or garbage collection. I don't see what your reply has to do with anything I actually said.

Jeroen, I was making a direct reply to Abhishek, who clearly said he was talking about Strings. I quoted him. The fact that I referred to "characters" would be another clue. Yes, we're talking about Strings. In what way was that not clear?
 
Screaming fools! It's nothing more than a tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic