Well, we didn't think this will create so much of confusion...
String a = "java"; //1
char[ ] b = { 'j', 'a', 'v', 'a' }; //2
String c = new String(b); //3
String d = a; //4
A) (a == d)
Obviously, because of //4. and '==' checks whether both the references point to same memory location or not.
B) (b==d)
Obviously not as b is an array of chars and d is a String! (This one was for c/c== programmers.)
C)(a == "java")
Whenever you create string using just "" (ie. without using new keyword) you get an interned String. 'interned' means the JVM uses the same memory location to provide for different references which want to refer to same string (content wise).
So, this is true as both a and "java" are actually stored in the same place.
But if you do new String("java"); you are explicitly telling the compiler to create/store this string in a seperate location. so a == c is not true.
Even if :
c = new String("java"); or c = new String(a);
(Take note, Jane)
D) a.equals(c) is true.
This is a standard method which is implemented such a way that it checks whether the contents of both the references are same or not.
HTH,
Paul.
------------------
http://pages.about.com/jqplus Get Certified, Guaranteed!
[This message has been edited by Paul A (edited September 14, 2000).]