• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Comparison of 2 Wrapper types.

 
Sama Willson
Greenhorn
Posts: 22
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

This is a snippet of code from an Enthuware test:


When compiled I get the error, "Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Incompatible operand types Short and Integer".

Why is that? and what does it mean?

Thanks in advance!
 
Gautham Muralidharan
Greenhorn
Posts: 23
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As, Short and integer are two different classes, you cannot compare their objects with one another.

 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sama Willson wrote:Why is that? and what does it mean?

Short answer: although a short can be assigned to an int, a Short and an Integer are incompatible types (like for example a Cat and a Dog). And the compiler prevents you from comparing two incompatible types, because these will never be equal to each other! That's a fairly common mistake.

In this topic you'll learn everything you need to know (and probably even more) about wrapper classes and the == operator (with a boatload of code snippets). Definitely worth reading!

Hope it helps!
Kind regards,
Roel
 
Claude Sylvanshine
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have in mind that there is no unboxing during the == operation.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Claude Sylvanshine wrote:Have in mind that there is no unboxing during the == operation.

That's not 100% accurate! And it depends on which operands you have on both sides of the == operator. If it is a primitive and a wrapper class, you'll definitely have unboxing; otherwise you don't (and a check is performed to see if both reference variables refer to the same object).
 
Claude Sylvanshine
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is correct, i meant for the case in his example . Apologies for not being clear enough.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic