• 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

Wrapper classes

 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Given that b and c refer to instances of wrapper classes, which two statements are true? (Choose two)
A. b.equals(b) returns true.
B. b.equals(c) returns the same result as b == c.
C. b.eqials(c) can return false even if c.equals(b) returns true.
D. b.equals(c) throws an exception if b and c are different wrapper types.
E. b.equals(c) returns false if the type of wrapper objects being compared are
different.
Answer: B, C

Why the answers A and C are false?
equals method does not compare the references.It compares the values (data).Here the value in B should be same as the value in B.
 
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
equals() is overriden in Wrapper classes and will return true iff
Compares this object to the specified object. The result is true if and only if the argument is not null and is an Integer object that contains the same int value as this object.

So if two instances of Integer/Wrapper classes are compared the result will be true iff both of them contain the same value.
Also if two instances of different wrapper classes are compared the result
will always be false.

So wrt above statement:
a) true
a.equals(a) will always be true


Which book or material has the above question you mentioned.

equals() is reflexive: for any non-null reference value x, x.equals(x) should return true.

b) false b==c will be true iff both b and c refer to same object
and b.equals(c) if both b & c are of same wrapper class type and contain same value

c)is false, because if c.equals(b) is true then b.equals(c) must be true.
It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true

d) b.equals(c) will return false if b and c reffer different Wrapper class types.

e)true.

So as per me the answer is a and e. I hope other ranchers agree to me.
I hope you to agree with me.

Could you please mention the book or material that had the questions.

Thanks
Deepak
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic