my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question from Dan's Mock exam Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question from Dan Watch "Question from Dan New topic
Author

Question from Dan's Mock exam

Salima Lalani
Ranch Hand

Joined: Aug 15, 2002
Posts: 59
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

Joined: Jul 14, 2001
Posts: 375
Hi Salima ,
Try this link - click here
HTH
Ashish H.
Salima Lalani
Ranch Hand

Joined: Aug 15, 2002
Posts: 59
Thanks Ashish,
Why strings are called immutable?
Where can i find more details on this topic?
Pls reply
Thanks
Salima
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
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

Joined: Jun 17, 2002
Posts: 154
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

Joined: Aug 29, 2002
Posts: 223
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


Whatever doesn't kill us ...<br />Is probably circling back for another try.<br />SCJP 1.4
Alan Phillips
Ranch Hand

Joined: Aug 13, 2002
Posts: 73
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

Joined: Aug 13, 2002
Posts: 73
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

Joined: Sep 06, 2002
Posts: 43
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


_ _____ _ <br />Used to be a Java Programmer but now I work on Microsoft Technologies - Word, Excel and Outlook!
Jonathas Carrijo
Greenhorn

Joined: Jul 25, 2002
Posts: 25
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...


Java has reached Brazil =D
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
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

Joined: Aug 15, 2002
Posts: 59
Thanks for all your help.

Salima
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question from Dan's Mock exam
 
Similar Threads
garbage collection
Where did it go wrong?--1
final String
If statement
garbage collection eligibility