Java Data Type (I)

Source: Internet
Author: User
Tags float range

Java Data Type (I)

1. Diagram of java data types:

Ii. Explanation of Basic java data types and value range

(1) Integer type

1. byte type

Byte occupies 8 digits in the computer, and the byte is a signed integer. When expressed in binary, the highest digit is 0, representing positive number 1, representing negative number. The default value is 0.

Value Range:-128 to 127 (the power of-2 is 7 to the power of 2 minus 1)

Extension (Value Range Calculation ):

Positive numbers exist in the form of original codes in the computer, and negative numbers exist in the form of supplementary codes in the computer, that is, the original codes of the absolute values of negative numbers are converted into two
And then add 1 after bitwise.

Let's look at the binary representation of-128 absolute value 128: 1000 0000 bitwise decimal 0111 plus 1: 1111 1000 0000, that is,-128 on the computer

The value is 1000 0000. Let's take a look.-129 in the computer indicates that the absolute value of 129 has exceeded the byte bit.
Number. So pay attention to this type of problem.

2. short Type

Short (short integer) a short 16-bit, value range:-32768 ~ 32767. In the memory, 2 bytes are occupied (the 15th power of-2 to the 15th power of 2-1). The default value is 0.

3. int type

Int (integer) an int 32-bit. The value range is (-2147483648 ~ 2147483647). The memory occupies 4 bytes (the 31 Power of-2 to the 31 power of 2-1). The default value is 0.

4. long TYPE

Long (long integer) is a 64-bit long value. The value range is (-9223372036854774808 ~ 9223372036854774807), occupies 8 bytes in the memory (63 of-2)

63 to the power of 2-1) 0 l or 0 L is recommended by default.

We can see that the value range of byte and short is relatively small, while the value range of long is too large. The occupied space is mostly int, which can satisfy our daily calculation.

Int is also the most commonly used Integer type. Generally, if an integer such as 35 appears in JAVA, this number is int type, but if you want

The long type must be followed by "L ".

 

In-depth byte test (similar to other types ):

Public static void main (String [] args ){


/**
* Value assignment of byte type: a negative value must be preceded by a "-". If it is not specified, it indicates a positive number (-indicates a negative number, + indicates a positive number, + can be omitted)
* If the first digit of a numeric value (positive or negative number) is not 0, the first digit of a decimal value (positive or negative number) contains 0 (excluding the first digit of 0x), it indicates that it is octal (for example: 011)
* If a numeric value (positive or negative) First contains 0x, it indicates that it is in hexadecimal format (for example, 0x11 or 0X11)
* Converting octal, decimal, and hexadecimal forced types to byte will result in loss of precision. The byte value range is-128 to 127.
*/


// Octal (9 in decimal format). The output value is 9.
Byte byte_bajinzhi = 011;
// Octal (-9 in decimal format). The output value is-9.
Byte byte_bajinzhi_fushu =-011;
// Octal (585 in decimal format, beyond the byte value range). The output value is 73 in decimal format (0111 in octal format)
Byte byte_bajinzhi_zhuanhuan = (byte) 01111;
// Decimal. The output result of this value is not 111.
Byte byte_shijinzhi = 111;
// Decimal, beyond the byte value range. The output result is-127.
// Note: the storage of positive numbers in the computer is the original code of this positive number; the original code of the absolute value of negative numbers stored in the computer is converted to binary and then reversed by bit and followed by 1
// Calculation process: 129 stored on the computer, the original code is: 127 + 2 (10000001) (Note: 127 is 01111111, plus 2 after 10000001,
// 10000001 is the storage of the computer. It can be seen that this value is a negative number. Remove the first 1 and convert it to the console. The conversion method is 1 minus 0000000, then the bitwise result is reversed to 1111111 (127), and the final output result is-127)
Byte byte_shijinzhi_zhuanhuan = (byte) 129;


// Hexadecimal (17 in decimal format). The output value is 17.
Byte byte_shiliujinzhi = 0x11;
// Octal (-17 in decimal format). The output value is-17.
Byte byte_shiliujinzhi_fushu =-0x11;


System. out. println ("byte_bajinzhi:" + byte_bajinzhi );
System. out. println ("byte_bajinzhi:" + byte_bajinzhi_fushu );
System. out
. Println ("byte_bajinzhi_zhuanhuan:" + byte_bajinzhi_zhuanhuan );
System. out. println ("byte_shijinzhi:" + byte_shijinzhi );
System. out. println ("byte_shijinzhi_zhuanhuan :"
+ Byte_shijinzhi_zhuanhuan );
System. out. println ("byte_shiliujinzhi:" + byte_shiliujinzhi );
System. out
. Println ("byte_shiliujinzhi_fushu:" + byte_shiliujinzhi_fushu );


}

The test results are as follows:

Byte_bajinzhi: 9
Byte_bajinzhi:-9
Byte_bajinzhi_zhuanhuan: 73
Byte_shijinzhi: 111
Byte_shijinzhi_zhuanhuan:-127
Byte_shiliujinzhi: 17
Byte_shiliujinzhi_fushu:-17

(2) floating point type

1. float Type

The memory structure is as follows:

1bit (symbol bit) 8 bits (index bit) 23 bits (tail bit)

2. double Type

The memory structure is as follows:

1bit (symbol bit) 11 bits (index bit) 52 bits (tail bit)

Therefore, the float index range is-128 ~ + 127, while the double index range is-1024 ~ + 1023, and the index bit is divided by complement code.
The negative index determines the smallest non-zero number of absolute values that floating point numbers can express. The positive index determines the maximum number of absolute values that floating point numbers can express, that is, the value range of floating point numbers.
Float range:-2 ^ 128 ~ + 2 ^ 127, that is,-3.40E + 38 ~ + 3.40E + 38; the double value range is-2 ^ 1024 ~ + 2 ^ 1023, that is,-1.79E + 308 ~ + 1.79E + 308.

3. Precision Discussion

The precision of float and double is determined by the number of digits of the ending number. Floating point numbers are stored in the memory in scientific notation, And the integer part is always an implicit "1". Since it remains unchanged, it cannot affect the accuracy.
Float: 2 ^ 23 = 8388608, a total of seven digits, because the leftmost 1 is omitted, this means that the maximum number can represent 8 digits: 2*8388608 = 16777216. There are 8 Valid digits, but the absolute guarantee is 7 digits, that is, float

Precision: 7 ~ 8-digit valid number; double: 2 ^ 52 = 4503599627370496, a total of 16 digits. Likewise, the accuracy of double is 16 ~ 17 digits.

For decimal places, errors are more likely to occur due to precision.

Test:

Test code:

Float f = 2.2f;
Double d = (double) f;
System. out. println (d );
F = 2.25f;
D = (double) f;
System. out. println (d );

Test results:

2.200000047683716
2.25

Testing Result Analysis -----------------------------------------------------------------------------------------------------------------------------

First, let's take a look at the single-precision storage method of 2.25. The conversion to binary bitwise is 10.01, which is very easy to organize into 1.001*2.
As a result, we can write 2.25 of the memory distribution:
Symbol bit: 0
The index is 1, indicating 0000 0001 with a supplementary code, and the transfer code is 1000 0001.
The ending number is 0010 0000 0000 0000 0000 000
The dual precision of 2.25 means: 0 100 0000 0001 0010 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 2.25, so that the value of does not change during forced conversion, and we try again

Let's take a look at "2.2". "2.2" should be represented by the scientific Notation: Convert the decimal to the decimal number x 2, and take the integer part, so 0.282 = 0.4, therefore, the first part of the binary decimal point is an integer of 0.4, Which is 0, 0.4 × 2.

= 0.8, the second digit is 0, 0.8*2 = 1.6, the third digit is 1, 0.6*2 = 1.2, the fourth digit is 1, 0.2*2 = 0.4, the fifth digit is 0, which will never be multiplied to = 1.0. The obtained binary is an infinite loop arrangement of 0011001100110011.

0011... for Single-precision data, the ending number can only represent the precision of 24 bit, so 2.2 of float storage is

However, in this storage mode, the value converted to decimal is not 2.2, because decimal conversion to binary may be inaccurate, such as 2.2, the double type data also has the same problem, so there will be some errors in the floating point representation. When the single precision is converted to the double precision, there will also be errors, the output result of the following code is different:

  1. Floatf = 2.2f;
  2. Doubled = (double) f;
  3. System. out. println (f );
  4. System. out. println (d); for decimal data that can be expressed in binary, such as 2.25, this error does not exist, so the above strange output results will appear.

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.