| Author |
adding two short numbers error
|
Marius Constantin
Ranch Hand
Joined: Nov 23, 2011
Posts: 62
|
|
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
Joined: Apr 06, 2010
Posts: 3791
|
|
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
Joined: Nov 23, 2011
Posts: 62
|
|
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
Joined: Jan 03, 2004
Posts: 5844
|
|
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
Joined: Nov 23, 2011
Posts: 62
|
|
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 !
|
 |
 |
|
|
subject: adding two short numbers error
|
|
|