Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Interesting, can anyone please explain.  RSS feed

 
Dirk Botha
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone please explain to me the following:



-------------------------------------
So here is the question, why is the rule for wrappers not being comparable,
inforced for the "==" operator.

But not for the "<" and ">" operators.
I would have been happy if I could blame the compiler, but then I
noticed that it not only compiles. It also works!!!

Then why, when wrapper classes are supposed to be incomparable...

Ofcourse, if my compiler, VM and I are both sane, this means that one
would be able to create a generic class that would easily deal with testing
equality between wrapper classes.

Thanks in advance,
Dirk.

PS. Yes, I'm busy studying for the SCJP 5. Hence the question.
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason that short1 == int1 won't compile is that when they are boxed to Short and Integer, you have the normal rule that == can only be applied to object references where one is a subtype of the other.

In the comparison operators > and <, the Short and Integer are unboxed and normal numeric promotion rules apply.
[ September 26, 2006: Message edited by: Keith Lynn ]
 
Srinivas Kalvala
Ranch Hand
Posts: 257
Firefox Browser Hibernate Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

In AutoBoxing , widening will not happen but same type can be compared.

Refer:
http://java.sun.com/j2se/1.5.0/docs/guide/language/autoboxing.html
 
Dirk Botha
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yup, thanks all, much appretiated.

Dirk.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!