It's not a secret anymore!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes sierra/bates ch.4 self test, question #2 (Combination Autoboxing & (Non-)Equality Operator) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "sierra/bates ch.4 self test, question #2 (Combination Autoboxing & (Non-)Equality Operator)" Watch "sierra/bates ch.4 self test, question #2 (Combination Autoboxing & (Non-)Equality Operator)" New topic
Author

sierra/bates ch.4 self test, question #2 (Combination Autoboxing & (Non-)Equality Operator)

Wout Er
Greenhorn

Joined: Nov 26, 2012
Posts: 15
The following compiles because the Long is unboxed and primitive comparison is used.



However, reading this, I was wondering whether autoboxing is ever used with the equality and non-equality operators (i.e., to obtain two object reference variables)?

The following does NOT compile due to incompatible types so apparently at least in this case, autoboxing is not used:


Thinking about it, it would make sense never to do autoboxing for the equality operator, since autoboxing typically implies a new object being created. Consequently, this new object will never be equal to another one that existed before the autoboxing took place.
Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 721
I believe autoboxing only applies to boxing primitve value to its corresponding wrapper object or vice versa, but not to any other object type.
Wout Er
Greenhorn

Joined: Nov 26, 2012
Posts: 15
Himai Minh wrote:I believe autoboxing only applies to boxing primitve value to its corresponding wrapper object or vice versa, but not to any other object type.


Maybe I didn't make myself clear enough. The equals (==) operator, in general, works on either 2 primitives, or 2 object references. If you use it with a primitive and a wrapper object, the wrapper object is auto-unboxed and the primitive version of the operator is employed. In my example, I used the equals operator with a primitive and an Object instance, and the primitive was not auto-boxed for comparison.

My question is whether a primitive will ever be auto-boxed when you pass it as an argument to an equality or a non-equality operator while the other argument to the operator is an object reference?
Joe Allen
Ranch Hand

Joined: Nov 14, 2012
Posts: 93
I'd think it'd work if you cast o to Integer before doing a comparison.
System.out.println(((Integer)o) == 2);
System.out.println(((Integer)o) != 2);


"There are no dumb questions" quoted from HFSJ. "To err is human; to forgive is divine"
OCPJP 6, OCEJWCD 6
Wout Er
Greenhorn

Joined: Nov 26, 2012
Posts: 15
joe alien wrote:I'd think it'd work if you cast o to Integer before doing a comparison.
System.out.println(((Integer)o) == 2);
System.out.println(((Integer)o) != 2);


That works indeed, but that's not my question.. For my question, 'o' can be any Object instance (not just wrapper-type instances).
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: sierra/bates ch.4 self test, question #2 (Combination Autoboxing & (Non-)Equality Operator)
 
Similar Threads
Compiling errata for K&B, SCJP 6
operator
Comparison of arrays
doubt in kathy sierra question?
Do you include the references in the equals