I would like to know what is exactly happening here.

if("String".toString() == "String")
System.out.println("Equal");
else
System.out.println("Not Equal");
The answer is "Equal"
My assumptions:
String literal is treated like a primintive.
so when u say "String" , is it not added to the String pool.
but when u say "String".toString() , is not a String object implicitly
being created , So the reference has changed .
I assume that toString() method is returning the 'string value' ,
which will now point to the "String" literal in the pool.
so the answer "Equal" is correct.
please clarify.
thanks in advance,
jayalv

Can I know if String objects and String literals are
treated the sameIn the Javadoc of the class java.lang.String, it is said like this:
  String str = "abc";is equivalent to:
char data[] = {'a', 'b', 'c'};
String str = new String(data);"
http://java.sun.com/j2se/1.4.1/docs/api/java/lang/String.html

Similar Messages

Maybe you are looking for