aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Another equals-question (K&B) Chapter 6, Question 3 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Another equals-question (K&B) Chapter 6, Question 3" Watch "Another equals-question (K&B) Chapter 6, Question 3" New topic
Author

Another equals-question (K&B) Chapter 6, Question 3

sebastian tortschanoff
Ranch Hand

Joined: Mar 19, 2009
Posts: 68
Given:




How does following equals method behave?



What makes this statement "true"?

x is an reference to an IntegerObject with the value 343.
when equals get's called on x, with parameter "343" what happens?

343 seems to be autoboxed to an IntegerObject. Since equals can take an Argument of type Object, 343 will be autoboxed.

Now what is equals testing? I think the references here are not relevant, but the type and the value. Type and value are equal - so far so good.

I thought there would be an equals test on references. Why isn't that so?


lookat this:



Strings seems also to be tested by type and value.
But Arrays seems to be tested by reference.


Power from within.

Failed SCJP 2 times :-(
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032


Here, the actual method being used is Integer.equals(Object). Since 343 is an int, it is first boxed into an Integer, and then reference widening takes place from Integer to Object. Integer overrides equals(Object) so that an Integer reference will be equal to an Object reference only if the Object reference actually points to an Integer object, and if both Integer objects contain the same value (which in this case is true.) The reason why there is not a == test on the references themselves is because Integer overrides Object.equals(Object).

Strings also override Object.equals(Object), but arrays do not (although you can use the Arrays.equals() and Arrays.deepEquals() static methods of the java.util.Arrays class.)


All code in my posts, unless a source is explicitly mentioned, is my own.
sebastian tortschanoff
Ranch Hand

Joined: Mar 19, 2009
Posts: 68
So Interger x is not widened to Object, too? It simpy overrides the equals method. Is that true?
Ryan Beckett
Ranch Hand

Joined: Feb 22, 2009
Posts: 192


x is of type Integer, and 343 is initially an int. It gets boxed into an Integer, and then the comparison takes place. All wrappers override Objects.equals and Object.toString. So, they are compared correctly.



String overrides Object.equals, so the two are compared correctly.



There is something called a String pool. When JVM recognizes a String literal it's placed into this pool. And, if the same literal appears again in the code its reference points the pre-existing string, making this condition true. This is Java's scape route for String immutability. If the pool didn't exist we'd have more out of memory errors.



I am not ultra sure about this one, but I think Object.equals is used here, because Java treats all arrays as objects. We know Object.equals compares references, so its false.




And, of course the two arrays are distinct objects with distinct addresses. False.
sebastian tortschanoff
Ranch Hand

Joined: Mar 19, 2009
Posts: 68
Hey Ryan,

thanks for your detailed depiction. This is veeeery nice.

Good night @ ALL
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Another equals-question (K&B) Chapter 6, Question 3