File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes adding two short numbers error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "adding two short numbers error" Watch "adding two short numbers error" New topic
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: 4467
    
    8

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: 6109
    
    6

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 !
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: adding two short numbers error