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.
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.
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.
SCJP, SCWCD, SCBCD
"How many licks ..." - I think all of this dog's research starts with these words. Tasty tiny ad: