Java basic Data Type knowledge summary

Source: Internet
Author: User
Tags binary to decimal decimal to binary

List of Java basic data types
Native Type number of occupying positions is there a sign bit Minimum Value Maximum Value Default Value Packing class Notes
Boolean 1 No —— —— False Boolean The Boolean type is the lonely queen she can't convert with other types
Byte 8 Yes -2^7=-128=byte.min_value 2^7-1=127=byte.max_value 0 Byte Frequently used in IO streams
Char 16 No ' \u0000 ' =0=character.min_value ' \uffff ' =2^15*2-1=65535=character.max_value ' \u0000 ' Character
Short 16 Yes -2^15=short.min_value 2^15-1=short.max_value 0 Short Historical legacy types, no longer being used
Int 32 Yes -2^31=integer.min_value 2^31-1=integer.max_value 0 Integer
Long 64 Yes -2^63=long.min_value 2^63-1=long.max_value 0 Long Use in cases where int cannot be stored
Float 32 Yes 1.4e-45f=float.min_value 3.4028235e38=float.max_value 0.0f Float Use when high-precision requirements for memory requirements are not high
Double 64 Yes 4.9e-324=double.min_value 1.7976931348623157e308==double.max_value 0.0d Double
Table Description:

1.nEm represents N*10 's M-square, and ne-m represents one of N*10 's M-sub-points.
2.Java Core Technology Volume One said: "Java does not have any unsigned type" is wrong, in fact, the Boolean and char is indeed an unsigned type.
3.Java Core technology Volume one indicates that the value range of the float type is ±3.40282347e38 clearly written incorrectly.

A character-related knowledge code unit is a numeric range of type char, and 0-65535 is a unit of code. The code point is a code unit that exceeds 65535, at which point Char cannot represent a character greater than 65535 because of the maximum limit of the char type.

To solve this problem, the development of the javaapi of the siege lion will be more than 65535 characters unified with two code units (0-65535), and stipulates that the two code units are good base friends, is a whole, we call them code point good!
In order to have all the characters have a unified name, the API of the siege lion is also very hard, the Siege lions simply specify that 0-65535 of the code unit can also call code point!
Here is the code unit and Code point operation code:

Char[] C = Character. Tochars(65536);String str = new String (c);System. out. println("I'm a code point, don't believe you see:"+ str. Codepointcount(0Str. Length()));//1System. out. println("I am the value of this code point:"+ str. Codepointat(0));System. out. println("I am two units of code, do not believe you see:"+ str. Length());System. out. println("I am the first unit of code:"+ (int) str. CharAt(0));System. out. println("I am the first unit of code:"+ (int) str. CharAt(1));String test ="A"+ str +"B";for (int i =0; i < test.length (); i++) {System. out. println("The code unit that traverses the string is doing this:"+ (int) test. CharAt(i));}for (int i =0; i < test.length (); i++) {int codepoint = Test. Codepointat(i);System. out. println("The code point that traverses the string is like this:"+ codepoint);if (Character. Issupplementarycodepoint(codepoint)) {i++;}}
Why isn't it recommended to use char?

String c1= "";//mathematical integer set symbol, which cannot be represented by Char because it exceeds 65535, so it can only be represented by two char (unit of code).
String str=c1+ "A";
Char C2=str.chartat (1);
The result C2 is not the ' A ' you want, but the latter half of the character (code unit).

For more information about code unit code points, see:
Code points and code units in Java

How Java stores negative numbers 1. How to store negative numbers

Java uses the "2 complement" (complement) to store negative numbers.
A number 00000001, such as 8 digits, indicates positive 1,11111111 negative 1.

2. Complement

The complement is based on the sign bit (based on the sign bit), the positive complement equals the number itself (this sentence should be interpreted as follows: not a positive complement equals itself, but positive numbers do not need to calculate its complement, positive calculation of the complement is for negative numbers, so the complement of the positive is equal to itself).

3. "2 of the complement"

"2 of the complement" is a number of the anti-code +1.

4. Anti-code

Anti-code is the bits of all the numbers, 0 to 1, 1 to 0, a positive inverse code equals the number itself (this sentence to understand: not a positive inverse code equals itself, but a positive number does not need to calculate its anti-code, positive calculation of the inverse code is for negative numbers, so a positive number of anti-code equals itself).
such as 00000001 of the anti-code is 11111110.

5. "1 of the complement"

"1 of the complement" does not have to scrutiny it, because "1 of the complement" results and anti-code consistent, but it is not "bits all take the reverse" mode of operation. We don't have to worry about how it is counted, it is right to interpret it directly as a counter code.

6. Place symbol bit

The digit sign bit is the method that represents the positive or negative in the binary, the highest bit of a number is the sign bit, if the sign bit is 0, if the sign bit is 1 for negative.

7. Intuitive notation

Intuitive, easy-to-understand representation of binary negative numbers is to place the sign bit, without the need for complement.
A number 00000001, such as 8 digits, indicates positive 1,10000001 negative 1.

8. Why use complement to denote negative numbers

The reason why the use of complement to express negative numbers, rather than directly using the intuitive and easy to understand the sign bit, is because the visual representation in the computer does not get the correct result in negative arithmetic, and complement can do.
Such as:
Number 00000001 (visual representation of the number 1)
+10000001 (visual representation of number-1)
=10000010 (visual representation of number-2)
Obviously 1+ (-1) =0 instead of-2 AH!!!
The correct result can be obtained by using the complement method:
Number 00000001 (complement notation of the number 1)
+11111111 (number-complement notation of 1)
Note that at this point only 8 bits, more than 8 bits will overflow, so:
=00000000 (complement notation of the number 0)

Basic Type Conversions

1.boolean is incompatible with any other type.
2. When a small type is converted to a large type, if the small type is not a char type, then the unsigned extension is performed, and if the small type is char, the unsigned extension is never executed.
Signed Extensions:
byte b= "00000001";
Short s=b;//at this time the value of S is 0000000000000001
byte b= "11111111";//11111111 is the complement form of byte type-1
Short s=b;//at this time the value of S is 1111111111111111,1111111111111111 is the short type-1 of the complement form.
Unsigned extensions:
Char c= "1111111111111111";
int i=c;//The value of I at this time is 00000000000000001111111111111111.
Char c= "0000000000000001";
int i=c;//The value of I at this time is 00000000000000000000000000000001.
3. Casts are required for large type conversions to small types.

Floating-point type related knowledge 1. Floating-point types have a large storage range

Although float is the same as the int placeholder, but float can store a value much larger than int, see Integer.max_value and Float.max_value, not only that, float is much larger than long, and the double is larger than float. Therefore, a long can not be stored in the number of float,float cannot be placed in a double.

2. Floating-point types cannot accurately represent decimals

float and double are not completely accurate at the time of calculation, float can be accurate to 6 or 7 decimal places, double can be accurate to 15 decimal places.
Decimals that are out of range are rounded.
For example:

System. out. println(1.0f/0.3F;System. out. println(2.0f/0.3F;System. out. println(1.0d/0.3D;System. out. println(2.0d/0.3D;Print:3.33333336.66666653.33333333333333356.666666666666667
3. Binary cannot accurately represent some decimals that can be accurately represented in the 10 binary

Binary cannot accurately represent some numbers in decimal, just as decimal cannot accurately represent 1/3, the binary cannot accurately represent 0.1 and so on various decimals.
(The following is the copy)
A binary representation of decimals can only represent any combination of 1/(2^n).
For example:
0.5 can be expressed as it can be expressed as 1/2.
0.75 can also be expressed as it can be expressed as 1/2+1/(2^2).
0.875 can also be expressed as it can be expressed as 1/2+1/(2^2) +1/(2^3).
0.1 cannot be expressed precisely because it cannot be expressed as a form of 1/(2^n).

4. Floating-point types are prone to deviations when calculating

There are two reasons:
1. Because the precision of the floating-point type is fixed, the accuracy of the out-of-range is rounded.
2. Because the binary cannot accurately represent some of the numbers in the decimal, you will see very strange results, such as:

System.out.println(1.0-0.93);System.out.println(1.0-0.7);System.out.println(1.1-1.0);打印:0.06999999999999995//计算机无法表达0.070.30000000000000004//计算机无法表达0.30.10000000000000009//计算机无法表达0.1

To use a fully accurate calculation, use BigDecimal, and this is the constructor of BigDecimal (String val).

5. Do not use = = To determine the size of two floating-point numbers

For the size of floating-point type judgment, should not use = =, but only use >, <, >=, <=, willing to above the 4th has been said.
Example:

float f1=1.345f;float f2=1.123f;System.out.println(f1+f2-f2==f1);//打印false

For more information about floating-point numbers, see: Where's your decimal point?
How decimals are stored in the computer

Binary floating-point numbers and decimal floating-point numbers convert decimal to binary binary:

Integer part, see figure:

For the decimal part, look at the picture:

Binary goto decimal:

Binary to decimal is relatively simple, multiply all the face value of the binary multiplied by 2 of the "literal value corresponding to the right system" the second side add, the language is not very good expression, look at the formula:
N1* (2^m2) +n2* (2^m2) +n3* (2^m3) + ...
For example:
1101.01=1*2 (2^3) +1*2 (2^2) +0* (2^1) +1* (2^0) +0* (2^-1) +1* (2^-2) =8+4+0+1+0+0.25=13.25.

Java basic Data Type knowledge summary

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.