Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

adding two short numbers error

 
Marius Constantin
Ranch Hand
Posts: 62
Java Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


compiler error : possible loss of precision
secondNum = firstNum + 2;
^
required: short
found: int
1 error

I am guessing that the compiler thinks that if firstNum is the maximum value of short, then adding 2, will result in a number too large to store in a short, so secondNum must be of type int.

Is this it ? But it doesn't make much sense in another way...

Please advice !

Thank you for your time, help, and patience,

Kind regards,
marius
 
Matthew Brown
Bartender
Posts: 4567
8
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any operation between two variables of integer type that are smaller than int results in an int. So:
short + short -> int
short + byte -> int

etc.

It's mentioned in the Java Language Specification.

The reason you can get away with firstNum++ is that the ++ operator has an implicit cast back to the type of the variable it was performed on. So it's actually the equivalent of:
firstNum = (short)(firstNum + 1);

 
Marius Constantin
Ranch Hand
Posts: 62
Java Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:Any operation between two variables of integer type that are smaller than int results in an int. So:
short + short -> int
short + byte -> int

etc.

It's mentioned in the Java Language Specification.

The reason you can get away with firstNum++ is that the ++ operator has an implicit cast back to the type of the variable it was performed on. So it's actually the equivalent of:
firstNum = (short)(firstNum + 1);



So in other words, I'm right

Thank you so much Mattew !
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marius Constantin wrote:
Matthew Brown wrote:Any operation between two variables of integer type that are smaller than int results in an int. So:
short + short -> int
short + byte -> int

etc.

It's mentioned in the Java Language Specification.

The reason you can get away with firstNum++ is that the ++ operator has an implicit cast back to the type of the variable it was performed on. So it's actually the equivalent of:
firstNum = (short)(firstNum + 1);



So in other words, I'm right


Sorry, but no.
I am guessing that the compiler thinks that if firstNum is the maximum value of short, then adding 2, will result in a number too large to store in a short, so secondNum must be of type int.

is not the same as
"The result of addition of any two integer types is of type int." Even if the compiler knows that the result will fit in a short, the result is still an int.
 
Marius Constantin
Ranch Hand
Posts: 62
Java Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:
Marius Constantin wrote:
Matthew Brown wrote:Any operation between two variables of integer type that are smaller than int results in an int. So:
short + short -> int
short + byte -> int

etc.

It's mentioned in the Java Language Specification.

The reason you can get away with firstNum++ is that the ++ operator has an implicit cast back to the type of the variable it was performed on. So it's actually the equivalent of:
firstNum = (short)(firstNum + 1);



So in other words, I'm right


Sorry, but no.
I am guessing that the compiler thinks that if firstNum is the maximum value of short, then adding 2, will result in a number too large to store in a short, so secondNum must be of type int.

is not the same as
"The result of addition of any two integer types is of type int." Even if the compiler knows that the result will fit in a short, the result is still an int.


you're right Jeff. Sorry folks. Case closed.

thank you very much for the help !
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic