How is the range of int values in Java calculated?

Source: Internet
Author: User

First, the JDK defines an int as 4 bytes ===> 32 bits (all subsequent calculations are based on this)

32 bits 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, these 32 squares, or 32 bits of space, can identify 10 binary numbers:

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;

The second method of calculation: Since there are 2 of the 32-way algorithm, then the largest number in accordance with the 10 binary is 2 32, or 4294967296;


Unfortunately, the above calculation is unsigned, which is a positive number, but in Java there are positive and negative points. So 32 squares occupy a lattice mark plus or minus. So you can only use 31 squares 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

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

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.