How is the range of int values in Java calculated?

Source: Internet
Author: User

The first definition of an int in the JDK is 4 bytes ===> 32 bits (all subsequent calculations are based on this)

The 32-bit is the JVM that allocates only 32 squares of space for storing data.

It is well known that computers store data in 0 and 1.

Then, the 32-lattice 0 or 1 method has 2 32-time species:

So. In these 32 squares. Or a 32-bit space that identifies 10 decimal digits:

Minimum

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Biggest

1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

The first method of calculation: The binary maximum number (32 1) is converted to 10 binary, that is, 4294967296;

Another method of calculation: Since there are 2 of the 32-way algorithm, then the largest number according to the 10 binary is 2 32 times. i.e. 4294967296;


Pity. The above calculation is unsigned. is a positive number. But in Java, there are positive and negative points in Int. So 32 squares occupy a lattice mark plus or minus.

So only 31 squares can be used to identify values.

X 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
X is 0 to identify positive or negative.


The last int can be identified by the maximum/minimum number is: 2 of 31 Square: +/-2147483648

How is the range of int values in Java calculated?

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.