Java short value range and usage

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.