aspose file tools*
The moose likes Java in General and the fly likes Issue with equality and autoboxing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Issue with equality and autoboxing" Watch "Issue with equality and autoboxing" New topic
Author

Issue with equality and autoboxing

Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4462
    
    6

[shameless plug]Please see my blog entry [/shameless plug] about the issue of losing transitivity of equality with autoboxing, discussed in a recent JavaWorld article. Does my reasoning hold water or am I way out in left field on this?
[ June 15, 2004: Message edited by: Junilu Lacar ]

Junilu - [How to Ask Questions] [How to Answer Questions]
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
I think the idea that autoboxing should cause == to work on object content for wrapper classes rather than on the object references (as you seem to imply) is a bad one.
It would effectively break the wrapper classes out of the Java object hierarchy and force overloaded operators to be introduced for them.

The situation is bad enough trying to explain to people why == shouldn't be used to compare Strings, now if it SHOULD (or could) be used to compare Objects of some other classes that would get a lot worse.


42
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4462
    
    6

Originally posted by Jeroen Wenting:
I think the idea that autoboxing should cause == to work on object content for wrapper classes rather than on the object references (as you seem to imply) is a bad one.


Are you saying that:





To me, the latter is more in line with the stated purpose of autoboxing/unboxing which is to eliminate the drudgery of manually converting from primitive to wrapper and vice versa. I would think that if you wanted the former and still have autoboxing, you'd have to write



instead. If (a == b) were equivalent to (a.equals(new Integer(b)), not only are you automatically converting the type of the variable b, you're also changing the == operator to behave like a call to the equals() method of the Wrapper class. That would be even more confusing because it's doing two things rather than just the one thing of unboxing the int value from a and then performing the == operation as normal.
Igor Ko
Ranch Hand

Joined: Jun 24, 2002
Posts: 90
I think auto boxing/unboxing don't worth for such language complication.

Now you should spend more time to understand simple program, and for what ?
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4462
    
    6



Think about it, you don't say Anti-lock Braking Systems are a bad thing for cars because some people don't know to keep stepping on the brake pedal to make it work right. Likewise, I don't think we should be too quick to judge autoboxing/unboxing because stupid things can be done with them.
[ June 16, 2004: Message edited by: Junilu Lacar ]
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4462
    
    6

Here's a stupid trick that I tried using JDK 1.5.0 beta 2:



Just because you can do it doesn't mean you should.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Issue with equality and autoboxing