Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Very Important String Questions

 
Liang Anmian
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I really don't understand this concept at all. Look at the following code:



When I run the code, the message actually prints out! I thought the equality operator tests only if 2 references refer to the same object? I clearly thought that the JVM will create 2 different strings in this scenario. After a while, I thought that maybe it's because the JVM searches the String Pool and realize that it can reuse the same string for the second reference, that's why the if statement becomes true.

BUT, what about this:



Now the message doesn't come out! What is happening?

What if there's a question asking me how many String objects are created altogether in a snippet of code? If there are identical strings in the code snippet, how do I know if the JVM will search the String Constant Pool? For example, this code snippet is from my study guide:



So according to the code above, how many String objects are created? Is it 3 or 4? It can be 4 because the first line may create 2 strings, one for variable x, and one for the method argument. But it can also be 3 because if the JVM reuses the same String object from the String Constant Pool, then only 1 String object is created for the first line.

Or am I being paranoid here, since the String Constant Pool may not be playing a part here. Haha!
[ January 13, 2005: Message edited by: Liang Anmian ]
 
Nathaniel Stoddard
Ranch Hand
Posts: 1258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String literals in your source code will only be created once and in one place: the constant string pool.

So, "String" == "String" ... will make only one actual String in the constant string pool, which is why the equality operator returns true. I didn't read your whole post. Hopefully you were still asking about the same thing further down.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please check the recent posts in this forum, this has been discussed in depth not so long ago. If you are lucky the topic may even have a relevent title.

And also read very carefully the String class API for the trim() method.
[ January 13, 2005: Message edited by: Barry Gaunt ]
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Basically, String literals in code will have the String be created once on the String Constant Pool.

Mark
 
Vivek Mongolu
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
code:

if ("String ".trim() == "String") System.out.println("Equal");

trim() wud create a new String object on the heap and since they refer to two different objects it prints out nothing.

code:

String x = new String("xyz");
y = "abc";
x = x + y;

This will actually create 4 objects
"xyz" and "abc" literals in String pool

initial x object referring to "xyz" and another object after concatenation
x object referring to "xyzabd". both these are on heap.

hope this is clear
 
Surendra Kumar
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does the x reference refers to xyz on the string pool, then in that case it is only one object, which means in total only 3 objects.
 
Jay Pawar
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
x will be created on the heap that is the object 1
the remaining 3 objects are

xyz
abc
xyzabc

they will be created in String literal pool.

Hope that helps you.
 
Surendra Kumar
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you mean the object on the heap created from

String x = new String("xyz");

is different from the "xyz" on String pool?



 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Surendra Kumar:
Do you mean the object on the heap created from

String x = new String("xyz");

is different from the "xyz" on String pool?





Yes, it is a different object. Read the String class API for the String(String) constructor.
[ January 14, 2005: Message edited by: Barry Gaunt ]
 
Liang Anmian
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops. Forgotten to thank you guys for the replies. They are very helpful.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic