Java is a strongly typed language. There are eight basic data types in Java, including four integer types, two floating-point types, one character type used to represent unicode encoding, and one character type used to represent the true value.
1. Basic storage space. Byte -- 8 bits, short -- 16 bits, int -- 32 bits, long -- 64 bits, float -- 32 bits, double -- 64 bits. The six numeric types are signed. Fixed storage space is one of the reasons why Java is portable and cross-platform!
2. The existence of basic types leads to Java OOP's non-pure modularity. Because the basic type is not an object, it is a small lie that everything is an object. This is a trade-off between execution efficiency.
3. Use the formula-2 (number of digits-1) power to 2 (number of digits-1) power-1 determines the range of integer types (byte, short, Int, long ).
4. Char is a 16-bit Unicode character or a 16-bit unsigned integer ranging from 0 to 65535. Even so, you can forcibly convert invalid data, such as: Char C1 = (char) 10000; char C2 = (char)-200 ;. This can be understood from the perspective of binary storage.
5. integers are octal (integers starting with 0), decimal, and hexadecimal (integers starting with 0x or 0x.
6. Char can use single quotes to represent a single character, for example, 'liang '. You can also use the Unicode value '"ucafe' (four-digit hexadecimal number ).
7. boolean. Boolean values can only be true or false, and test whether it is true or false. It cannot perform any other operations or convert to another type.
Positive example: Boolean b1 = 1> 2; negative example: int seen = button. isvisible ();
Practice: conciseness is a virtue. Do not write it like this: If (is = true & done = false). Only new users can write it like this.
For any programmer if (whether &&! Done. So remove all = fasle and = true.
8. The default floating point type is double. To create a float, you must add f or F to the floating point. For example, float Pi = 3.14; is incorrect.
9. The default Integer type is int. To use a long integer, add "L" or "L" to it, for example, 1000l. (Lowercase L is easily mistaken for 1, not recommended)
10. Float can be precise to 7 valid digits. A 8th-digit number is obtained by rounding out the 9th-digit number. A double can be precise to 16 valid digits, the fourth digit is rounded to the fifth digit. How much does Gates have? It must be indicated by double. Float cannot be used ......
11. If you require accurate answers, do not use float and double because they are carefully designed to provide accurate and fast approximation within the wide area value range. However, they do not provide completely accurate results. This is especially inappropriate for currency computing, because a float or double is required to accurately express 0.1 (or any 10)
12. biginteger supports Integers of any precision. Bigdecimal supports the number of points with any precision.
13. Initialization cannot be emphasized! Java provides default initialization for all member variables: byte, short, Int, long -- 0 float -- 0.0f double -- 0.0 Boolean -- false char -- '"u000000 ', in particular, all references of the object type are initialized to null. (Note! Local variables except the array do not offer this kind of preferential treatment. You need to initialize them yourself. In addition, is the default initialization value what you want? So it is best to initialize the variable explicitly, usually in the constructor .)
14. Conversion between basic types. Java type check is very strict, from low-precision conversion to high-precision explicit conversion is not required, double D = 123 ;. But in turn, for narrow conversion, from high precision to low precision, or one type to another type, you must use forced type conversion. Java provides a secure conversion mechanism, but whether the results are expected or not is guaranteed by yourself.
Double D = 12.5;
Float F = (INT) D; // The result is 12 instead of 13!
When the floating point type is converted to an integer type, the number after the decimal point is truncated without rounding.
15. improvement. The results of mixed operations on various basic data types are the most expressive. For example, the result of int and long operations is long, and the result of integer and floating point operations is floating point. Note: As long as the type is smaller than INT (such as char, byte, and short), these values are automatically converted to int before calculation. Example:
Byte b1 = 12;
Byte b2 = b1 + 1; // an error occurred during compilation! Because B1 + 1 is already int type! Remember!
16. Scientific notation of floating point type. In mathematics, e Represents the natural logarithm (math. e Represents the double value), and in Java e Represents the power of 10. The float number indicates float F = 1e-27f. It indicates a power of 27 times multiplied by 1 by 10.
Public class primitivetypetest {
Public static void main (string [] ARGs ){
// Byte
System. Out. println ("basic type: byte binary digits:" + byte. size );
System. Out. println ("Packaging class: Java. Lang. Byte ");
System. Out. println ("minimum value: byte. min_value =" + byte. min_value );
System. Out. println ("maximum value: byte. max_value =" + byte. max_value );
System. Out. println ();
// Short
System. Out. println ("basic type: Short binary digits:" + short. size );
System. Out. println ("Packaging class: Java. Lang. Short ");
System. Out. println ("minimum value: short. min_value =" + short. min_value );
System. Out. println ("maximum value: short. max_value =" + short. max_value );
System. Out. println ();
// Int
System. Out. println ("basic type: int binary digits:" + integer. size );
System. Out. println ("Packaging class: Java. Lang. Integer ");
System. Out. println ("minimum value: integer. min_value =" + integer. min_value );
System. Out. println ("maximum value: integer. max_value =" + integer. max_value );
System. Out. println ();
// Long
System. Out. println ("basic type: Long binary digits:" + long. size );
System. Out. println ("Packaging class: Java. Lang. Long ");
System. Out. println ("minimum value: Long. min_value =" + long. min_value );
System. Out. println ("maximum value: Long. max_value =" + long. max_value );
System. Out. println ();
// Float
System. Out. println ("basic type: Float binary digits:" + float. size );
System. Out. println ("Packaging class: Java. Lang. Float ");
System. Out. println ("minimum value: float. min_value =" + float. min_value );
System. Out. println ("maximum value: float. max_value =" + float. max_value );
System. Out. println ();
// Double
System. Out. println ("basic type: double binary digits:" + double. size );
System. Out. println ("Packaging class: Java. Lang. Double ");
System. Out. println ("minimum value: Double. min_value =" + double. min_value );
System. Out. println ("maximum value: Double. max_value =" + double. max_value );
System. Out. println ();
// Char
System. Out. println ("basic type: Char binary digits:" + character. size );
System. Out. println ("Packaging class: Java. Lang. Character ");
// Output character. min_value to the console in the form of a number rather than a character
System. Out. println ("minimum value: character. min_value ="
+ (INT) character. min_value );
// Output character. max_value to the console in the form of a number rather than a character
System. Out. println ("maximum value: character. max_value ="
+ (INT) character. max_value );
}
}
Running result:
1. Basic Type: byte binary digits: 8
2. Packaging class: Java. Lang. byte
3. Minimum value: byte. min_value =-128
4. Maximum Value: byte. max_value = 127
5,
6. Basic Type: Short binary digits: 16
7. Packaging class: Java. Lang. Short
8. Minimum value: short. min_value =-32768
9. Maximum Value: short. max_value = 32767.
10,
11. Basic Type: int binary digits: 32
12. Packaging class: Java. Lang. Integer
13. Minimum value: integer. min_value =-2147483648
14. Maximum Value: integer. max_value = 2147483647
15,
16. Basic Type: Long binary digits: 64
17. Packaging class: Java. Lang. Long
18. Minimum value: Long. min_value =-9223372036854775808
19. Maximum Value: Long. max_value = 9223372036854775807
20,
21. Basic Type: Float binary digits: 32
22. Packaging class: Java. Lang. Float
23. Minimum value: float. min_value = 1.4e-45
24. Maximum Value: float. max_value = 3.4028235e38
25,
26. Basic Type: double binary digits: 64
27. Packaging class: Java. Lang. Double
28. Minimum value: Double. min_value = 4.9e-324
29. Maximum Value: Double. max_value = 1.7976931348623157e308
30,
31. Basic Type: Char binary digits: 16
32. Packaging class: Java. Lang. Character
33. Minimum value: character. min_value = 0
34. Maximum Value: character. max_value = 65535
The minimum and maximum values of float and double are both output in the form of scientific notation. The ending "e + number" indicates the number before e multiplied by 10. For example, 3.14e3 is 3.14 × 1000 = 3140, and 3.14e-3 is 3.14/1000 = 0.00314.
The basic types are stored in the stack, so their access speed is faster than the instance objects of the corresponding packaging class stored in the heap. From java5.0 (1.5), Java Virtual Machine (VM) can automatically convert the basic types and their corresponding packaging classes. Therefore, when assigning values, passing parameters, and performing mathematical operations, we use their packaging classes like using basic types, but this does not mean that you can call their packaging classes through basic types. In addition, the packaging classes of all basic types (including void) use final modifier, so we cannot inherit from them to extend new classes or override any of their methods.