• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

comparison doubt - K&B book

 
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
according to the K&B book, mock exam for chapter 6, Q4...

Integer x = 343;
long L343 = 343L;

if (x.equals(L343)) s+=".e1";

this return false. they got different types.



so why x.equals(y) return false, too? they are both Integer?!
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But the values are different (343 vs. 333). equals() does not only check if the types are the same.
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there,
well...x and y are two Integer objects , also the value of x and y is not same so x.equals(y) which refer to same content, returns false.

When dealing with wrapper classes Byte, Short, Integer deals with specified range resp. such that if the value fall in that range then two references refer to the same object
e.g
Integer i1 = 100;
Integer i2 = 100;
in this case i1.equals(i2) as well as i1 == i2 both will return true,
you can refer k&b for the ranges.
 
adam Lui
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i got further doubt on this issue.

Regarding Hashing, steps to retrieve(locate) an object are:

1.Find the right bucket (using hashCode() )
2. Search the bucket for the right element (using equals() )

ok, whats the effect using == instead on step 2?

i know this could be such a dumb question to many of java-ers here, but i reall y cant get through! pls help!
 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by adam lui:
i got further doubt on this issue.

Regarding Hashing, steps to retrieve(locate) an object are:

1.Find the right bucket (using hashCode() )
2. Search the bucket for the right element (using equals() )

ok, whats the effect using == instead on step 2?

i know this could be such a dumb question to many of java-ers here, but i reall y cant get through! pls help!





for Object type, equals() compares the values (333/343), not the types. the type must be the same to invoke the method. if the values aren't the same, it will return false, if the types aren't the same, it won't compile.

== when used to compare Object types compares the actual object references to see if they refer to the same Object.
Ovject ob1 = new Object();
Object ob2 = ob1;
 
reply
    Bookmark Topic Watch Topic
  • New Topic