aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Very Important String Questions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Very Important String Questions" Watch "Very Important String Questions" New topic
Author

Very Important String Questions

Liang Anmian
Ranch Hand

Joined: Jun 25, 2004
Posts: 119
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 ]

Current Status:<br /> <br />SCJP 1.4<br />SCJD (in progress)
Nathaniel Stoddard
Ranch Hand

Joined: May 29, 2003
Posts: 1258
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.


Nathaniel Stodard<br />SCJP, SCJD, SCWCD, SCBCD, SCDJWS, ICAD, ICSD, ICED
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
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 ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

Basically, String literals in code will have the String be created once on the String Constant Pool.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Vivek Mongolu
Ranch Hand

Joined: Apr 17, 2003
Posts: 42
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

Joined: Apr 20, 2003
Posts: 21
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.


Cheers,<br />Surendra
Jay Pawar
Ranch Hand

Joined: Aug 27, 2004
Posts: 411
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.


Cheers,<br />Jay<br /> <br />(SCJP 1.4)<br />Heights of great men were not achieved in one day, they were toiling day and night while their companions slept.
Surendra Kumar
Greenhorn

Joined: Apr 20, 2003
Posts: 21
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

Joined: Aug 03, 2002
Posts: 7729
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

Joined: Jun 25, 2004
Posts: 119
Oops. Forgotten to thank you guys for the replies. They are very helpful.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Very Important String Questions