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

String Question

 
Kay Liew
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question from MindQ's

11. The statement ...

String s = "Hello" + "Java";

yields the same value for s as ...

String s = "Hello";
String s2= "Java";
s.concat( s2 );

Answer is false ?!? Could that be a mistake or have I mis-understood the question ?

 
osman cinar eren
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It really is false. that is about the String class in Java. String objects are IMMUTABLE. that is:

s.concat(s2) creates a new object whose value is equal to s+s2, but as this new object is not referenced by any variable, it is unreachable. On the other hand, the original object referenced by 's' remains unchanged.

You should read on the concept of String.

hope this helps.
 
meena latha
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
s i do agree....
If u dont create a object reference to the string the content will be lost.
But with StringBuffer it doesnt happens like that.

StringBuffer s= new StringBuffer("welcome");
s.append("here");
System.out.println(s);

output
Welcome here.
 
Kay Liew
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand that String is immutable. But the question as is the "value" the same and not are they the same or "==",

To me, values mean the final print out which in this case both resulted HelloJava.

Ok, i guess the problem is how one define "value". In this case, I thought it is asking about the final printout and not asking about "==" or equal() .
 
Steven Bell
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The final value in the two would not be the same.
You missed one thing in your code.

 
Kay Liew
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steven,

If you do not alter my original code.



What is the printout of One and Two ? Now isn't that the "value" the same ?
thx.
k
 
Steven Bell
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But the question was 'yields the same value for s'

when you do
System.out.println(s.concat(s2)
you are not printing the value of s, but the value returned by the concat method.
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In a very simplle manner :

"Hi ".concat("How are you");

will return ( what you are thinking is right ) :
Hi How are you

but you don't have any reference for this object , so this is immutable .

s.concat(s1) ; // don't think that result of this will be refereed by s ( it happens in StringBuffer , not in String ) .
[ January 27, 2005: Message edited by: rathi ji ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic