Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

compare short-values

 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have to short-values:

something like this returns false:



I guess, I know the difference of equals and identity? But can someone explain it in this scenario.


 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay I guess, I know the difference:

1==1 returns false, because e is identically not i

1.equals(1) returns true, because the value of e is equal of i.



 
Vijitha Kumara
Bartender
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Well, How did you check it? Did you able to compile it (with short primitive references)?
 
Sridhar Santhanakrishnan
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vijitha is right, the code will have compile errors as you cannot call equals() on primitives like short,int....

And i==e returns true.
 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I used it with Short:

Short i;
Short e;

Using primitive types, if(i==e) returns compiler-failure?
 
Vijitha Kumara
Bartender
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nimo frey wrote:Using primitive types, if(i==e) returns compiler-failure?


No. That should return true with the code you provided in the OP. But i.equals(e) gives you a compiler error if i and e are primitives.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49405
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please look at this FAQ. If you had written Short rather than short at the beginning of the post, the answer would have been obvious.
 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So when using primitive types, I can use clearly this:



When using Wrapper, I have to use equals instead of "==".

thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic