• 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

Clarify... Wrapper Classes...

 
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Integer i = new Integer (42);
Long l = new Long (42);
Double d = new Double (42.0);

Which expressions evaluate to True?
A. (i == l)
B. (i == d)
C. (d == l)
D. (i.equals (d))
E. (d.equals (i))
F. (i.equals (42))
G. (i.equals (l))
H. (l.equals (d))
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I assume you're using 1.5.

The equals methods in Integer, Long, and Double will not return true if the parameter to equals is not the correct class.

So I would guess f is the only one that is true. But you can type in a test class and determine if that's right.
 
Shiaber Shaam
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Need still more clarifications..............
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A. (i == l) //compile-time error
B. (i == d) //compile-time error
C. (d == l) //compile-time error
D. (i.equals(d)) //false because d is not an instance of Integer
E. (d.equals(i)) //false because i is not an instance of Double
F. (i.equals(42))//true
G. (i.equals(l)) //false because l is not an instance of Integer
H. (l.equals(d)) //false because d is not an instance of Long
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A. (i == l) //compiler error, compare objects of different class
B. (i == d) //compiler error, compare objects of different class
C. (d == l) //compiler error, compare objects of different class
D. (i.equals (d)) //false, compare objects of different class
E. (d.equals (i)) //false, compare objects of different class
F. (i.equals (42)) //true, 42 autoboxing to Integer and then compare contents
G. (i.equals (l)) //false, compare objects of different class
H. (l.equals (d)) //false, compare objects of different class

Method equals() for Wrapper classes always return false if compared objects diffrents class
P.S Sorry for bad English
 
Shiaber Shaam
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your Posts.....
 
reply
    Bookmark Topic Watch Topic
  • New Topic