• 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

Very Important String Questions

 
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 1258
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Basically, String literals in code will have the String be created once on the String Constant Pool.

Mark
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oops. Forgotten to thank you guys for the replies. They are very helpful.
 
She's out of the country right now, toppling an unauthorized dictatorship. Please leave a message with this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic