• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Wrapper class doubt

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have a silly doubt in Wrapper class.


Answer: D, E

But, when i ran the code, I got false for both D and E.
Can any1 explain me please?
 
Madhavan Santhanam
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And can any1 explain me abt the bevaviour of equal() and == in Wrapper class?.
Thank you!!
 
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The "==" operator checks to see if its operands refer to the same object.
The "equals()" compare values of objects.

In your example only F is correct, because the equals() retuns true only if the objects have the same value and of the same type.
 
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!!!

I think D, E, F will return false.

If i take: i.equals(d)

The method equals in the Integer's class look like this:



And since d is not an instance of Integer, the method will return false.

With the same reasoning,E and F will also return false.
 
Madhavan Santhanam
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the replies.
FYI, option f gave me a compile time error..
 
Collins Mbianda
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which version of java do you use ?

42 should be boxed into an Integer.
Make sure you have at least java 5.
 
Madhavan Santhanam
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oh,I m no sure abt the version I use, so for scjp1.4, what ll the answer?.WIll it be compile time error or not?
Thanks!!
 
ahmed yehia
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

This code if compiled under JDK 1.4 or earlier will give error because you cant pass an int i.e 42 in equals() when an object is expected.
under JDK 1.5 or later autoboxing takes care of converting the passed int to wrapper object in this case 42 becomes Integer of value 42.
so it returns true because you are comparing two Integers.
 
Madhavan Santhanam
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Similar is the case for Integer i =25 I guess.
Thanks for all your replies
 
Collins Mbianda
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes
Boxing and unboxing appear with java 5.0

With java 1.4 you cannot write

Integer i = 25;
or (new Integer(25)).equals(25);

If you are preparing for SCJP 1.4, you will not need this new concepts.
 
Collins Mbianda
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Madhavan Santhanam:
oh,I m no sure abt the version I use, so for scjp1.4, what ll the answer?.WIll it be compile time error or not?
Thanks!!



To know which version you are using, open a commands' console
and write: java -version
 
Madhavan Santhanam
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks!!!
 
And then we all jump out and yell "surprise! we got you this tiny ad!"
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic