Variable: number of files
Statement: -------- it is equivalent to opening an account in the bank
int a;//Declare an integer variable named a int b,c;//Declare two integer variables named b, c //int a;// Compilation error, variable cannot have the same name
Initialization: that is, the first assignment ------------ is equivalent to saving money for the account
int a = 520;//Declare the integer variable a and assign it 520 int b;//Declare integer variable b b = 520;//Assign 520 to variable b b = 52;//Modify the value of variable b to 52 int c = 5,d = 10;//Declare two integer variables c and d and assign values of 5 and 10 respectively
Use: ----------- use the money in the account
The use of a variable is the use of the number it stores
int a = 5; int b = a+10;//Take the value 5 of a, add 10, and assign it to variable b System.out.println(b);//Value of output variable b 15 System.out.println("b");//Output b, output as is in double quotation marks a = a+10;//Add 10 on the basis of a itself int c = 5000;//Account balance c = c-1000;//Withdrawal 1000
Variables must be declared and initialized before they can be used
//System.out.println(m);// Compilation error, variable m not declared int m; //System.out.println(m);// Compilation error, variable m initialization
Naming:
- Can only contain letters, numbers_ And $, and cannot start with a number
- Strictly case sensitive
- Keywords cannot be used
- Chinese naming is allowed, but not recommended. It is recommended to "see the name and know the meaning in English" and "small hump naming method"
The code is as follows:
int a_5$,_3c,$_; //int a*b;// Compilation error, cannot contain special symbols such as * sign //int 1a;// Compilation error, cannot start with a number int aa=5; //System.out,println("aA); / / compilation error, case sensitive //int class;// Compilation error, keyword cannot be used int Age;//Yes, but not recommended int age;//"See the name and know the meaning in English" int score,myScore,myJavaScore;//"Small hump nomenclature"
Eight basic data types: byte, short, int, long, float, double, boolean and char
int: integer, 4 bytes, - 21 billion to 21 billion
- The integer direct quantity is of type int by default, but cannot exceed the range. If it exceeds the range, a compilation error occurs;
- When two integers are divided, the result is still an integer, and the decimal places are unconditionally discarded (not rounded);
- During integer operation, overflow occurs if it exceeds the int range (overflow is not an error, but it should be avoided)
int a = 25;//25 is called integer direct quantity, and the default type is iny //int b = 10000000000;// Compilation error. 10 billion is of type int by default, but it exceeds the int range //int c = 3.14;// Compilation error, integer variables can only contain integers System.out.println(5/2);//2 System.out.println(2/5);//0 System.out.println(5/2.0);//2.5 int d = 2147483648;//Maximum value of int d = d+1; System.out.println(d);//-2147483648 (int minimum), overflow occurred, overflow needs to be avoided
Long: long integer, 8 bytes, very large
- Long integer direct quantity needs to add L or l after the number;
- If overflow is possible during operation, it is recommended to add L after the first number
long a = 25L;//25L is a long integer direct quantity //long b = 10000000000;// Compilation error. 10 billion is of type int by default, but it exceeds the int range long c = 10000000000L;//100L is a long integer direct quantity //long d = 3.14;// Compilation error. Long integer variables can only be integers //If overflow is possible during operation, it is recommended to add L after the first number long e = 1000000000*2*10L; System.out.println(e);//20 billion long f = 1000000000*3*10L; System.out.println(f);//Not 30 billion long g = 1000000000L*3*10; System.out.println(g);//30 billion
double: floating point, 8 bytes, very large
- The floating-point direct quantity is double by default. If you want to represent float, you need to add f or F after the number
- When double and float data are involved in the operation, rounding errors may occur, which cannot be used in precise occasions
double a = 3.14;//3.14 is a floating point number, and the default is double float b = 3.14F;//3.14F is float type direct quantity double c = 1234000000000000000000000000000000000.0; System.out.println(c);//1.234E33, expressed by scientific counting method, is equivalent to 1.234 * (the 33rd power of 10) double d = 3.0,e = 2.9; System.out.println(d-e);//0.10000000000009, rounding error may occur, and cannot be used in precise occasions
boolean: boolean, 1 byte
Can only be assigned to true or false
boolean a = true;//True is Boolean direct quantity ----------- true boolean b = false;//False is Boolean direct quantity ----------- false //boolean c = 250;// Compilation error, Boolean can only be assigned to true or false
char: character type, 2 bytes
- Unicode character set encoding format is adopted. One character corresponds to one code. The form is character char, but it is essentially code int (between 0 and 65535);
- ASCII code: 'a' - 57, 'a' - 65, '0' - 48
- The character type direct quantity must be placed in single quotation marks and can only have 1
- Special symbols need to be escaped through \
char c1 = 'female';//Character female char c2 = 'f';//Character f char c3 = '6';//Character 6 char c4 = ' ';//Space character //char c5 = female// Compilation error, character literal must be placed in single quotation marks //char c6 = '';// Compilation error, must have characters //char c7 = 'female'// Compilation error, can only have 1 character char c8 = 65;//Between 0 and 65535 System.out.println(c8);//When println is output, it will be displayed according to the data type of c8 //If c8 is char type, characters are displayed //If c8 is int type, the number is displayed char c9 = '\\'; System.out.println(c9);//\
Conversion between types:
The basic types from small to large are: byte - short - int - long - float - double - char
There are two ways:
- Automatic / implicit type conversion: small type to large type
- Cast: large type to small type
Syntax: (data type to convert to) variable
Strong rotation may overflow or lose accuracy
int a = 5; long b = a;//Automatic type conversion int c = (int)b;//Cast type long d = 5;//Automatic type conversion double e = 5;//Automatic type conversion long f = 10000000000L; int g = (int)f;//Cast type System.out.println(g);//1410065408, strong rotation may overflow double h = 25.987; int i = (int)h;//Cast type System.out.println(i);//25. Strong rotation may lose accuracy
There are two rules:
- Integer direct quantity can be directly assigned to byte, short and char, but it cannot exceed the range;
- When byte, short and char data participate in the operation, the system will automatically convert them to int for re operation
byte b1 = 5;//byte ranges from - 128 to 127 byte b2 = 6; byte b3 = (byte)(b1+b2); System.out.println(2+2);//4 System.out.println(2+'2');//52,2 plus' 2 'code 50 System.out.println('2'+'2');//100, code 50 for '2', plus code 50 for '2'
Data type classification:
- Reference data type
- Basic data type
Memory size:
1G=1024M (megabytes)
1M=1024KB (kilobytes)
1KB=1024B (bytes)
1B=8bit (bit)