Java binary and data type (ii)

Source: Internet
Author: User

I. Maximum and minimum values for each data type

Integer:

In the case of byte, we know that byte has 8 bit bits, the maximum value is 0111111, the minimum value is 10000000, and the decimal is -128~127, that is, -2^7~2^7.

According to the reasoning above,

Summarize the following table:

Data type
Bit bits Range of values
Byte 8 -2^7~2^7-1
Short 16 -2^15~2^15-1
Int 32 -2^32~2^32-1
Long 64 -2^63~2^63-1

Decimal:

We know that float is a 32-bit, double-64-bit, which is called single-precision and double-precision decimals, respectively. But their maximum value is not determined by the above code, we use the code to see what their maximum value is:

SYSTEM.OUT.PRINTLN ("Maximum value of Float:" +float.max_value); SYSTEM.OUT.PRINTLN ("Maximum value of Double:" +double.max_value);

Operation Result:

Maximum value of float: 3.4028235e38double Maximum: 1.7976931348623157E308

Through the above code, we know that the maximum value of float is 3.4*10^38,double maximum value is 1.79*10^308.

Why is it that the same 32-bit int and the 64-bit long cannot be represented?

Let's take a look at the storage structure:

Because the binary is more troublesome, we use decimal to express.

The storage of integers is simple, the first bit is the sign bit, and the rest of the bits represent values, such as

0 9 9 9

The first bit is the sign bit, and the next three bits are the digits, so that means 999.

However, fractional storage methods are not the same:

Sign bit Point Digit (order code bit) Trailing digits (decimal digits)

As a double first and an integer is the sign bit, followed by a total of 11 digits, the remaining digits are all the trailing digits, in double as an example, double the number of digits is 52 bits.

Or just the value:

0 9 9 9
The first 0-bit sign bit, the first "9" refers to the digits, the following two 9 means the decimal point is 0.99, then this number is 0.99*10^9.

This is why the decimal number of the same digit is larger than the number represented by the integer.

Second, accuracy

We first use int and float to represent the same number:

int a = 12345678;float B = 12345678; SYSTEM.OUT.PRINTLN ("int:" +a); System.out.println ("float:" +b);

Operation Result:

Int:12345678float:1.2345678e7

At this point, the values of int and float are the same, and when they represent a larger number, the following problems occur:

int a = 123456789;float B = 123456789; SYSTEM.OUT.PRINTLN ("int:" +a); System.out.println ("float:" +b);

Operation Result:

Int:123456789float:1.23456792e8

At this point, float loses a number 8, and now we use a double to represent the number:

Double C = 123456789; System.out.println ("Double:" +c);

Operation Result:

Double:1.23456789e8

We are surprised to find: Double no data loss!

As we said above, float is a 32-bit, double is 64-bit, float the reason for the loss of data is because the number of floats is not enough to store 123456789, so some data is lost, but the double has 64 bits, The number of decimal digits is sufficient to represent 123456789, so there is no data loss.

Iii. Forcing data type conversions

High precision, low accuracy

The loss of precision occurs when the high precision is converted to low precision, for example:

Double A = 123.45;int B = (int) A; System.out.println (b);

Operation Result:

123

It is visible that the fractional part is discarded when the double is converted to int.

High-Low

High-bit data loss occurs when the number of bits is reduced, for example:

int a = 129;byte B = (byte) A; System.out.println (b);

Operation Result:

-127

We know that int is 32 bits, and 129 is represented by binary means 00000000 00000000 00000000 10000001. In the strong to byte, because byte only 8 bits, will the first 24 bits all out, the remaining is 10000001, to decimal is-127.

Java binary and data type (ii)

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.