• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question from Dan's Mock exam

 
Salima Lalani
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Why strings are called to be immutable?
Can somebody explain me why the result of this code is true and false
class Red {
public static void main(String args[]) {
String a = "A";
String b = "B";
System.out.print((("A"+"B")=="AB") + ",");
System.out.print(("A"+"B")==(a+b));
}
}

Thanks
Salima
 
Ashish Hareet
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Salima ,
Try this link - click here
HTH
Ashish H.
 
Salima Lalani
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ashish,
Why strings are called immutable?
Where can i find more details on this topic?
Pls reply
Thanks
Salima
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I do not understand it still. We are not re-assigning any string values (which is not allowed in case of String objects) but we are comparing two concating String objects. If I have:
System.out.print(a+b);
it prints AB.
But if I have:
System.out.print((a+b)==(a+b));
or:
System.out.print(("A"+"B")==(a+b));
both return false.. Why?
 
Vin Kris
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Barkat, A point to remember always is that the 'new String' constructor invocation always creates a new String object - be it any of the overloaded constructors.
In the case of ( (a+b) == (a+b) ), 2 new objects are created in the expression even though the values are the same. Therefore, it is false.
In the case of ( "A" + "B" == (a+b) ), The compiler concatenates the LHS to "AB" since it is a compile time constant and hence is an object belonging to the intern pool. Whereas the RHS, (a+b) is evaluated at run time and creates a new object from the heap memory - so false again.
Another good-to-remember point is that toString() method of String object returns a reference to self - i.e., 'return this;'.
 
Shishio San
Ranch Hand
Posts: 223
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,
public static void main(String[] args) {
String a = "AB";
System.out.println(("AB"==a));
}
the variable a and the compile time constant "AB" has different memory address (i believe). Shouldn't this return false ???
thx
 
Alan Phillips
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,
Strings are immutuable(unchangeable) which is why you can't use functions like append or insert etc. Keep in mind that the overloaded concatenation operand is actully a method which does all the conversion and things for you. When you add two strings together or a string plus a float or whatever what really happens is:
code =
1.String s = "abc";
2.s = s + "def";
In line one a String is created in memory and s points to it.
2) In line 2 the + method is called. It is overloaded to take floats, ints, Strings whatever. It CONVERTS the string "abc" to a StringBuffer which are changeable. Then it runs the function StringBuffer.append("def") then it converts the StringBuffer back to a String object.
String s = "abc";
StringBuffer s1 = s;
s1.append("def");
s=s1;
So, the content of s is never changed, just transfered to a StringBuffer and then replaced!
 
Alan Phillips
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shinsio,
When using = and Strings, the jvm checks if it already has a String like that in memory and then points to that same spot again if it has another String that matches.
String a = "AB";
String b = "AB";
both of these point to the same "spot". When using the new constructor ie:
String a1 = new String("AB");
String b1 = new String("AB");
Distinct Objects are created in memory so, the equals operator figures this out and compares the strings for you. The following is true:
a == b //true
a.equals(b) //true
a1==b1//false
a1.equals(b1)//true
Try it out with some of your own code, it's easy to see that way!
 
Bishal P
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am adding this interesting code from the JLS.

and the compilation unit:

produces the output:
true true true true false true

What you have to remember is that there is a pool of strings created when you use a construct like
String s = "abc".
Now any further reference to abc (at compile time) will point to the same reference. Infact if you have "a" + "bc" in your code, it will be computed at compile time and a reference will be pointing to "abc".
Hope this made things clearer.
Thanks
Bishal
 
Jonathas Carrijo
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I think there's a rule of thumb that could help here. Whenever a string can be doubtlessly evaluated at compile-time, it will be! That requires you NOT to use any object reference variables!
Take the code Salima wrote, and observe the lines:
5. System.out.print((("A"+"B")=="AB") + ",");
6. System.out.print(("A"+"B")==(a+b));
At line 5, the expression within the parenthesis is completely evaluatable at compile time! So it will be, and the resulting string will be imutably put in the pool of strings. If it already exists, no other copy is made!! The SAME string is reused.
At line 6, within the second parenthesis, we have variables!! Who can garantee the values pointed by those variables!? Other object's method, for example, can change their values before execution reaches line 6!! Thus, not-immutable strings are created at runtime, not in the string pool, but in the heap!
That's it, I guess...
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys:
My oppologies...I knew this stuff very well (i.e. == operater checks operand refereing to same object, while "equals" checks for same string representation of two objects). I probably had lapse of memory...
Thanks
Barkat
 
Salima Lalani
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all your help.

Salima
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic