Tuesday, August 3, 2010

equals(): TKO.java

Courtesy of SCJP Sun® Certified Programmer for Java™ 6 Study Guide Exam (310-065) (9780071591065)
Chapter 6, Question 3

Output of this program is -.e2 =s fly

.e1: does NOT print because you are using equals() method to compare Integer with long.

.e2: equals() method for integer wrappers will only return true if both operands are the same primitive type and the two values are equal

=s: it's ok to unbox s1 and use ==. If we would have used equals(), this would not be true because you are comparing Short to an int

fly: it's ok to create a wrapper object with an expression, and unbox it for comparison with a primitive

No comments:

Post a Comment