• Post Reply Bookmark Topic Watch Topic
  • New Topic

Wrapper Classes  RSS feed

 
Madhurendra Narayan Tiwary
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Integer i = 32;
Short s = 28;

why comparing i and s using "<" or ">" works fine but comparing them using "==" produces error?

example

if(i<s) //fine

if(i>s) //fine

if(i==s) //error
 
Ron McLeod
Bartender
Posts: 1602
232
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are trying to compare two objects which are not compatible. If you want to compare the numeric values of the two objects you could do this: (i.intValue() == s.shortValue())
 
Joanne Neal
Rancher
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to know why it works for < and > but not for == then you need to read the Java language Specification, specifically sections 15.20.1, 15.21.1 and (possibly) 15.21.3
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!