There are 8 basic types of Java
Integer type:
Byte (1 bytes or 8 bits)
Short (2 bytes or 16 bits)
Int (default integer type) (4 bytes or 32 bits)
Long (8 bytes or 64 bits), when the value of a long is expressed, the value must end with L or L
Floating-point types:
Float (4 bytes or 32 bits), when representing a value of float, you need to add F or f at the end of the value
Double Default (8 bytes is 64 bits)
Char type: (2 bytes or 16 bits)
In Unicode encoding format, its essence is still an integer
Boolean type:
True (1 bytes or 8 bits)
False default (1 bytes is 8 bits)
Type name |
BYTE space |
Scope of Use |
Byte |
1 bytes (8 bits) |
-27~27-1 is mainly used for storing byte data, more commonly |
Short |
2 bytes (16 bits) |
-215~215-1 rarely used |
Int |
4 bytes (32 bits) |
-231~231-1 default, storing ordinary integers, most commonly used |
Long |
8 bytes (64 bits) |
-263~263-1 long integers, which are more commonly used when int cannot be stored |
Float |
4 bytes (32 bits) |
Storing single-precision floating-point numbers |
Double |
8 bytes (64 bits) |
Store double-precision floating-point numbers, commonly used |
Char |
2 bytes (16 bits) |
0~65535 Store a character commonly used |
Boolean |
1 bytes (8 bits) |
Store logical variables (true, false) common |
Commonly used escape characters
The above basic types are from small to large: Byte-short-int (char)-long-float-double
1) from small type to large type, automatic type conversion can be done
2) from large type to small type, forced type conversion is required, but precision is lost
byteB1 = 10;//integers can be assigned directly in this range, and when the operation is required, it is actually converting B1 to int before the operation ShortS1 = 20;//integers can be assigned directly in this range, and when the operation is required, it is actually converting B1 to int before the operation intI1 = 30; I1=S1; S1= I1;//compilation requires a forced type conversionS1 = ( Short) I1; LongL = 40L;//At this point 40 is a long type, in order to clearly indicate in the post plus L floatF1 = 3.45;//Compile Error floatF2 = 3.45F; DoubleD1 = 4.4; DoubleD2 = 2; System.out.println (D1-D2);//output is 2.4000000000000004 double type is non-precision operation CharC1 = 65;//char type is an integer typeSystem.out.println (C1); C1= 97; SYSTEM.OUT.PRINTLN (C1); BooleanBOOL =true; BooleanBool2 =false;
Basic types of Java Basic Learning