• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Comparison of 2 Wrapper types.

 
Greenhorn
Posts: 22
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 32
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As, Short and integer are two different classes, you cannot compare their objects with one another.

 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have in mind that there is no unboxing during the == operation.
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is correct, i meant for the case in his example . Apologies for not being clear enough.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic