GeeCON Prague 2014*
The moose likes Java in General and the fly likes String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "String" Watch "String" New topic
Author

String

bagaria kushal
Greenhorn

Joined: Mar 29, 2011
Posts: 19
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

Joined: Jun 10, 2011
Posts: 207

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 ?


Piyush
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

Also, UseAMeaningfulSubjectLine. "String" isn't saying much about the problem.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
bagaria kushal
Greenhorn

Joined: Mar 29, 2011
Posts: 19




the output was


false
true
false



please explain
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39053
    
  23
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

Joined: Oct 02, 2003
Posts: 11351
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
bagaria kushal
Greenhorn

Joined: Mar 29, 2011
Posts: 19
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

Joined: Oct 27, 2005
Posts: 19697
    
  20

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

Joined: Oct 02, 2003
Posts: 11351
    
  16

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

Joined: Mar 29, 2011
Posts: 19
So does that means that s3 object is created using new operator???
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39053
    
  23
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

Joined: Mar 29, 2011
Posts: 19
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

Joined: Oct 27, 2005
Posts: 19697
    
  20

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

Joined: Mar 29, 2011
Posts: 19
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

Joined: Oct 27, 2005
Posts: 19697
    
  20

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

Joined: Oct 13, 2005
Posts: 39053
    
  23
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

Joined: Oct 27, 2005
Posts: 19697
    
  20

Yes, but I didn't want to make it more complicated for Bagaria at this time.
bagaria kushal
Greenhorn

Joined: Mar 29, 2011
Posts: 19
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

Joined: Oct 13, 2005
Posts: 39053
    
  23
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
 
subject: String