This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization 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
Posts: 48445
56
  • 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