The short 16-bit Max is + 2 ^ 15, Min-32768
Signed int-128 .. 128 1 8-bit
Inteter-32768 .. 32767 2 signed 16-bit
Longint-2147483648 .. 2147483647 4 with 32 characters
Byte 0 .. 255 1 with 8 characters
Word 0 .. 65535 2 signed 16-bit
Instance
Public class MainClass {
Public static void main (String [] arg ){
Short smallNumber = 1234;
System. out. println (smallNumber );
}
}
Short s = 1;
Why s + = 1,
S = s + 1 is wrong? Question added:
It turns out that the type does not match when values are assigned, rather than the type does not match when values are added,
S = (short) (s + 1 );
Analysis
First, let's see why "s = s + 1" is wrong.
The Java compiler considers a direct number like "1" to be an int type. Java's automatic type conversion is a type that occupies less space and can be automatically converted to a type that occupies a large space. Otherwise, the type conversion must be forced. Otherwise, an error is returned. Because the expression "s + 1" is added to the short and int types, it is automatically converted to the int type, in this case, an error is reported if "s = s + 1" is assigned to a short type without forced type conversion.
As for why s + = 1 is not wrong, I don't think it is necessary to study it in depth. Since you have discovered this fact, you can understand it, maybe the Java designer made special processing on it at the beginning.