I have code like this
short a = 1;
short b = 2 ;
short c = a + b; // dosen't compile
What is the reason for compilation failure? x + x always produces Integer or bigger Number, but why?
I have code like this
short a = 1;
short b = 2 ;
short c = a + b; // dosen't compile
What is the reason for compilation failure? x + x always produces Integer or bigger Number, but why?
Copyright © 2021 Jogjafile Inc.
None of the binary operators will produce an
Integer. However, it will use anintinstead of shorter types,byte,shortandcharIf the compiler can inline the value it can cast the value for your. e.g.The only operator which produces an
Integeris a cast.BTW: On oddity is that Java defines the 32-bit
floatas being "wider" than the 64-bitlongvalue. This has the downside that float has much less precision. Consider this.prints
Even though 0.0F was added to
lit was implicitly cast tofloat(as float is wider) and then cast back (as an operator assignment was used) resulting in a error of ~20 billion.