aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question from jqplus for equals() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question from jqplus for equals() " Watch "Question from jqplus for equals() " New topic
Author

Question from jqplus for equals()

K2Joshi
Greenhorn

Joined: Aug 23, 2000
Posts: 17
Hi All,
can any one please explain me why answer D is right.
Which expressions will evaluate to true if preceded by the following code?
String a = "java";
char[ ] b = { 'j', 'a', 'v', 'a' };
String c = new String(b);
String d = a;
A) (a == d) B) (b==d) C)(a == "java")D) a.equals(c)
The answer is A,C & D

[This message has been edited by K2Joshi (edited September 13, 2000).]
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
There is onlu one String onject with value of "java" existed after the first sataement executed. So, all the reference variable a, c, d, and the String litler "java" are with same value, they are not only equals() but == as well.
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
When you create an object its data is placed in the systems memory. Every memory location has it's own 'address' or reference.
It helps to think of memory as a filing cabinet. For example, when String object 'a' is created the JVM opens memory Drawer#1 and puts the characters 'java' inside. It then stores the location Drawer#1 in 'a'. When String 'c' is created, the characters 'java' are placed in Drawer#5 and the location Drawer#5 is stored in 'c'.
So if you say 'a == c' the answer is false because the object named 'a' is not in the same drawer (memory location) as the object named 'c'.
But, 'a.equals(c)' is true because the equals() method goes to the filing cabinet, looks in Drawer#1 and Drawer#5, and sees that both drawers (memory locations) hold the same characters 'java'.
Hope that helps


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
srikrish
Ranch Hand

Joined: Sep 12, 2000
Posts: 63
Jane,
Pl. correct me if I'm wrong but when String c is created, does not it not call the String.intern() method to check if the "java" object is already available in the String's pool? In that case, c will end up pointing to the same object that a is pointing (i.e. Drawer#1 according to your explanation). So, c==a will be true.
Am I right in my understanding?
Sandeep Potnis
Ranch Hand

Joined: Aug 18, 2000
Posts: 39
srikrish,
Whenever a String is created with the new keyword, a new String object is created at a new memory location. So c==a would be false.
Please correct me if I'm wrong
Thanks
Sandeep
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi guys ...
I think srikish may be right about String using the pool.
I am very sorry ... thought I understood this but obviously I don't. Please see my newest post.
Apologies for any confusion I've caused.
Paul A
Ranch Hand

Joined: Aug 25, 2000
Posts: 44
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).]


<A HREF="http://pages.about.com/jqplus" TARGET=_blank rel="nofollow">http://pages.about.com/jqplus</A> <BR>Get Certified, Guaranteed!
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hello, every1,
Sorry, I made a mistake yesterday. I'm almost understand. the following code:
class Test {
public static void main(String s[]) {
String a = "java";//line3
String b = new String("java");
System.out.println(a == b);
System.out.println(a.equals(b));
System.out.println(a == "java");//line7
System.out.println(a.equals("java"));
}
}
compile and run, the output is :
false
true
true
true
BUT when i modify the line3 to :
String a = new String("java");
the line7 will output false
Is the object referenced by "a" not a "interned" string? Can anybody help me?
Thanks.
Yiqing
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Question from jqplus for equals()