This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String

 
bagaria kushal
Greenhorn
Posts: 19
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String s3="hello"+"world";
String s4="helloworld";
System.out.println(s3==s4);

Please tell me what happens when we concatenat two string literals using + operator.
What will be the output of SOP. If false then why?
How many Objects are getting created here?
are objects created in string literal pool eligible for GC?
is any object created above is eligible for gc?

 
Piyush Joshi
Ranch Hand
Posts: 207
Eclipse IDE Firefox Browser jQuery
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bagaria kushal wrote:
What will be the output of SOP. If false then why?

Why don't you try executing the code, instead of keep guessing

and all of your questions have been answered here: Scjp Tip Line - Strings Literally

also some good discussion about literals: when to use string literal and String New Object ?
 
Rob Spoor
Sheriff
Pie
Posts: 20388
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, UseAMeaningfulSubjectLine. "String" isn't saying much about the problem.
 
bagaria kushal
Greenhorn
Posts: 19
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator




the output was


false
true
false



please explain
 
Campbell Ritchie
Sheriff
Pie
Posts: 47293
52
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you read the "String literally" link you were provided? Have you looked in the Java™ Language Specification? If you use ctrl-F-"string" you find this, which should help explain your question.
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12022
25
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well, the obvious answer is that s3 doe NOT equal s4, so you got the first false.

Then, s4 DOES equal s5, so you got true. and then, since s3 does NOT equal s5, you got false.

My point here is that people will put about as much effort into answering your question as you do in asking it.

What did you think the output SHOULD be? Why do you think this is wrong (assuming you do)? Do 2 of the 3 make sense, and you only need an explanation of one of them?

please show some effort when you ask a question. Make it as easy as possible for folks to know exactly what help you need, and you are more likely to get it.
 
bagaria kushal
Greenhorn
Posts: 19
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all I am sorry for not putting my full efforts while asking the question and for the not so clear Subject

well my curiousity behind asking these questions were how string objects are maintained in java.

Most of the places i have read that String s="hello" creates a object in the string literal pool but in the link provided above its written that the String s="hello will create object in the heap only.Now if the object gets created in the string literal pool, so refering to the above program "hello" object gets created, referenced by s1. and "world" referenced by s2.
s1+s2 creates a new object "helloworld" so when String s4="hello"+"world"; gets executed,s4 should get referenced to the earlier "helloworld" object(String literal pool concept) so why s3==s4 evaluates to false.
 
Rob Spoor
Sheriff
Pie
Posts: 20388
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because the compiler doesn't know that s1 is "hello" and s2 is "world" when creating s3. The compiler is quite forgetful - it doesn't remember any values for any variables. For s4 on the other hand it sees that both Strings occur within the same statement, and therefore s4 can have no other value than "helloworld". But after that value is assigned, it no longer knows that s4 is "helloworld".
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12022
25
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The compiler is not smart enough to know that "s1 + s2" is the exact same thing as "helloworld". Since s1 and s2 are variables, they could change. So, it has to create a new object at runtime.

Since s4 and s5 are literals, the compiler CAN optimize them into the same literal, and therefore the same object in the string pool.

So, s3 and s4 do indeed point to different objects, and are therefore not equal.
 
bagaria kushal
Greenhorn
Posts: 19
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So does that means that s3 object is created using new operator???
 
Campbell Ritchie
Sheriff
Pie
Posts: 47293
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bagaria kushal wrote:So does that means that s3 object is created using new operator???
No. It doesn't saynew anywhere in that line. It is created by the + operator.
 
bagaria kushal
Greenhorn
Posts: 19
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The thing which I am not getting is if s3 a new reference variable refering to helloworld object on the string literal pool(correct me if wrong) now if I do s4="hello"+"world"
the string becomes helloworld whose object is already present in string literal pool referenced by s3. So s4 should also refer to the object which s3 refers or there is another concept.
 
Rob Spoor
Sheriff
Pie
Posts: 20388
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bagaria kushal wrote:So does that means that s3 object is created using new operator???

Actually, yes, internally. You don't need to worry about the exact details*, but when you use + to concatenate two Strings (or a String and something else), a new String is created. That is, except if both sides of the + are literals; numeric literals, boolean literals or String literals. (Of course at least one needs to be a String literal.) In that case, the two literals are combined into another String literal. And that's exactly what happened with s4's value.


* If you really want to know, the compiler turns the assignment to s3 into this: String s3 = new StringBuilder().append(s1).append(s2).toString();
As you see, there is indeed a new object created, the StringBuilder object. It's toString() method also creates a new String object with the current contents.
 
bagaria kushal
Greenhorn
Posts: 19
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rob for the explanation.

So concatenation using + creates new Object every time even if the string is already present in the pool.


I hv some more doubts hope you guys wont mind.

String s="hello";
s=s.concat("world");

so how many objects will get create here? "hello", "world","helloworld" or just "hello" and "helloworld"

One more thing in the link provided above its written that the string objects are created in the heap only not in the string literal pool. But I jave read that objects get created in the pool.
Please clarify
 
Rob Spoor
Sheriff
Pie
Posts: 20388
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bagaria kushal wrote:Thanks Rob for the explanation.

So concatenation using + creates new Object every time even if the string is already present in the pool.

Except the case where both sides are literals, like "hello" + "world", or even "hello" + 5 or "hello" + true. These create new String literals "helloworld", "hello5" and "hellotrue".
 
Campbell Ritchie
Sheriff
Pie
Posts: 47293
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Isn't it slightly more complicated than literals on both sides of the +? Isn't it when both operands are compile-time constants?
 
Rob Spoor
Sheriff
Pie
Posts: 20388
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, but I didn't want to make it more complicated for Bagaria at this time.
 
bagaria kushal
Greenhorn
Posts: 19
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hv some more doubts hope you guys wont mind.

String s="hello";
s=s.concat("world");

so how many objects will get create here? "hello", "world","helloworld" or just "hello" and "helloworld"

 
Campbell Ritchie
Sheriff
Pie
Posts: 47293
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can work it out. When you load the class, the literals hello and world are loaded into the String pool. Later, when the code is executed. the third object helloworld is created.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic