• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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.)
 
sebastian tortschanoff
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So Interger x is not widened to Object, too? It simpy overrides the equals method. Is that true?
 
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Ryan,

thanks for your detailed depiction. This is veeeery nice.

Good night @ ALL
 
My first bit of advice is that if you are going to be a mime, you shouldn't talk. Even the tiny ad is nodding:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic