Event address: CSDN 21-day learning challenge
The biggest reason for learning is to get rid of mediocrity. One day earlier, there will be more splendor in life; one day later, one day more mediocrity. Friends, let's work together!
type of data
1 Classification of Java language data types
- basic data type
- Reference data type (learn in depth when object-oriented)
2 Four categories and eight types of basic data types
type of data | keywords | memory usage | Ranges |
---|---|---|---|
integer | byte | 1 | Negative 2 to the 7th power ~ 2 to the 7th power -1 (-128~127) |
short | 2 | Negative 2 to the 15th power ~ 2 to the 15th power -1 (-32768~32767) | |
int | 4 | Negative 2 to the 31st power ~ 2 to the 31st power -1 | |
long | 8 | Negative 2 to the 63rd power ~ 2 to the 63rd power -1 | |
floating point number | float | 4 | 1.401298e-45 ~ 3.402823e+38 |
double | 8 | 4.9000000e-324 ~ 1.797693e+308 | |
character | char | 2 | 0-65535 |
Boolean | boolean | 1 | true,false |
illustrate
e+38 means multiplying by 10 to the 38th power, similarly, e-45 means multiplying by 10 to the negative 45th power.
In java, integers are of type int by default, and floating point numbers are of type double by default.
Need to remember the following points
The value range of byte type:
-128 ~ 127
Approximate value range of int type:
-More than 2.1 billion~ More than 2.1 billion
The relationship between the value range of integer type and decimal type:
double > float > long > int > short > byte
The most commonly used data type selection:
-
When defining variables, different types of variables should be selected according to the actual situation.
For example: the age of a person, you can choose the byte type.
For example: the age of the earth, you can choose the long type.
-
If the range of the integer type is not sure, then the int type is used by default.
-
If the range of the decimal type is not sure, then the double type is used by default.
-
If you want to define a variable of character type, then use char
-
If you want to define a variable of Boolean type, then use boolean
3 Define 8 basic data type variables
public class VariableDemo3{
public static void main(String[] args){
//1.定义byte类型的变量
//数据类型 变量名 = 数据值;
byte a = 10;
System.out.println(a);
//2.定义short类型的变量
short b = 20;
System.out.println(b);
//3.定义int类型的变量
int c = 30;
System.out.println(c);
//4.定义long类型的变量
long d = 123456789123456789L;
System.out.println(d);
//5.定义float类型的变量
float e = 10.1F;
System.out.println(e);
//6.定义double类型的变量
double f = 20.3;
System.out.println(f);
//7.定义char类型的变量
char g = 'a';
System.out.println(g);
//8.定义boolean类型的变量
boolean h = true;
System.out.println(h);
}
}
important point
- If you want to define a variable of integer type, you don't know which data type to choose, and int is used by default.
- If you want to define a variable of decimal type, you don’t know which data type to choose, and double is used by default.
- If you want to define a variable of type long, you need to add the L suffix after the data value. (Upper and lower case are acceptable, but upper case is recommended.)
- If you want to define a variable of type float, you need to add the F suffix after the data value. (both uppercase and lowercase)