1, Basic use of arithmetic operators
1.+,-,*,/,%,++,–
int num1 = 10; int num2 = 5; int result = num1 + num2; System.out.println(result);//15 System.out.println(num1 - num2);//5 System.out.println(10 * 5);//50 System.out.println(10 / 5);//2 System.out.println(10 % 3);//1 //++: self increment 1 //++a: Increase by 1 before use int a = 10; System.out.println(++a);//11 System.out.println(a);//11 //b + +: use first and add 1 int b = 10; System.out.println(b++);//10 System.out.println(b);//11 //--: self subtraction 1 //--c: Subtract 1 first and then use int c = 10; System.out.println(--c);//9 System.out.println(c);//9 //d --: use first, and then subtract 1 int d = 10; System.out.println(d--);//10 System.out.println(d);//9
2. Drill down on arithmetic operators
//Case 1: byte type will be transformed upward into int byte b1 = 10; byte b2 = 20; //10 - byte - 8 bits: 00001010 //10 - int - 32 bits: 0000000000000000000000001010 //20 - byte - 8 bits: 00010100 //20 - int - 32 bits: 000000000 0100 //30 - int - 32 bits: 000000000 1110 //30 - byte - 8 bits: 00011110 byte result = (byte)(b1+b2); System.out.println(result); Case 2: short Type will be transformed upward into int short s1 = 10; short s2 = 20; short result = (short)(s1+s2); System.out.println(result); Case 3: describe the types of the following calculation results byte b = 10; short s = 10; int i = 10; long l = 10; float f = 1.1F; double d = 2.2; System.out.println(b+s);//int type System.out.println(i+s);//int type System.out.println(l+s);//long type System.out.println(i+l);//long type System.out.println(i+f);//float type System.out.println(i+d);//double type //Case 4: floating point type can't perform operation directly. BigDecimal will be used for operation double d1 = 0.5; double d2 = 0.4; System.out.println(d1-d2);//0.09999999999999998 //Case 5: 'a' ASCII is 97 char c = 'a'; System.out.println(c+1);//98
3. Classic interview questions
3.1. The output result is?
int a = 8; int b = (a++)+(++a)+(a*10); System.out.println(b);//118 //a = 10 //b = 8 + 10 + 10
3.2. The output result is?
int i = 0; i = ++i; //Underlying principle: //i = (int)(i+1); //i = i; System.out.println(i);//1 int i = 0; i = i++; //Underlying principle: //int temp = i;--temp is used to record I the initial value //i = (int)(i+1); //i = temp System.out.println(i);//0
3.3 extension
byte b = 10; ++b;//Bottom layer: b = (byte)(b+1); short s = 10; ++s;//Bottom layer: s = (short)(s+1); int i = 10; ++i;//Bottom layer: i = (int)(i+1); long l = 10; l++;//Bottom layer: l = (long)(l+1); int i = 10; //++i; And I + +; There is no difference, because the semicolon is the end of an execution statement. Add it to me whether you add it first or later //++i; //i++; System.out.println(i);
2, Assignment operator
=,+=,-=,*=,/=,%=
int i = 10; i += 10;//i = (int)(i+10); i -= 10;//i = (int)(i-10); i *= 5 ;//i = (int)(i*5); i /= 5 ;//i = (int)(i/5); i %= 3 ;//i = (int)(i%3); System.out.println(i);//1
1. Classic interview questions
1.1 the output result is?
//Declare multiple variables at once int a,b; a = b = 100;//Assign 100 to b and b to a System.out.println(a);//100 System.out.println(b);//100
1.2. The results of the following two writing methods are?
short s=1; s = s+1;//The literal type of the variable s of type int is the result of the operation of type s short s=1; s += 1;//s = (short)(s+1);
Assignment rules:
s + = 1 s = (T)((s) + (1) )
The compound assignment E1 op= E2 is equivalent to the simple assignment E1 = (T)((E1)op(E2)),
Where T is the type of E1.
3, Relational operator
==,!=,>,>=,<=
System.out.println(10 == 10);//true System.out.println(10 != 10);//false System.out.println(10 > 10);//false System.out.println(10 >= 10);//true System.out.println(10 < 10);//false System.out.println(10 <= 10);//true
1. Summary
1. = is the assignment number, and = = is to judge whether the two values are equal
2. The results of relational operators are boolean values
2. Classic interview questions
2.1. The output result is?
int x = 10; int y = 10; boolean flag = (x == y); System.out.println(flag);//true flag = (x = y);//Error: boolean is not compatible with other types System.out.println(flag);
2.2. The output result is?
boolean b1 = true; boolean b2 = false; boolean b3 = (b1 == b2); System.out.println(b3);//false boolean b4 = (b1 = b2); System.out.println(b4);//false
4, Logical operator
& and & & short circuit and
| or | short circuit or
^ XOR
! wrong
//&And: both are boolean values. If it is true at the same time, the result will be true System.out.println(true & true);//true System.out.println(true & false);//false System.out.println(false & true);//false System.out.println(false & false);//false //&&Short circuit and: both are boolean values, and the result is true only when it is true System.out.println(true && true);//true System.out.println(true && false);//false System.out.println(false && true);//false System.out.println(false && false);//false //& vs && //&: judge the former as false before judging the latter //&&: if the former is judged as false, the latter will not be judged, so the efficiency is higher //ArithmeticException - arithmetic exception System.out.println(false && 10/0>5); //|Or: both are boolean values. If one is true, the result will be true System.out.println(true | true);//true System.out.println(true | false);//true System.out.println(false | true);//true System.out.println(false | false);//false //||Short circuit or: both are boolean values. If one is true, the result will be true System.out.println(true || true);//true System.out.println(true || false);//true System.out.println(false || true);//true System.out.println(false || false);//false //| vs || //|: judge whether the former is true or the latter //||: if the former is true, the latter will not be judged, so the efficiency is higher System.out.println(true || 10/0>5); //^XOR: both are boolean values, false for the same and true for the different System.out.println(true ^ true);//false System.out.println(true ^ false);//true System.out.println(false ^ true);//true System.out.println(false ^ false);//false //! Non negative inversion boolean bool1 = true; System.out.println(!bool1);//false boolean bool2 = false; System.out.println(!bool2);//true
1. Cases of logical operators
Enter a number in the dos window to judge whether it is within the range of 50 ~ 100
//1. Enter an int number Scanner scan = new Scanner(System.in); System.out.println("Please enter a int Value:"); int num = scan.nextInt(); //2. Judge whether it is within the range of 50 ~ 100 boolean bool = num>50 && num<100; //3. Output results System.out.println("Is the value at 50~100 Within the range of:" + bool);
5, Use of Scanner class
1. Meaning:
Java provides us with a class whose function is to input data on the console
2. Code
`````//Create an object of the Scanner class //human beings Li Dong = new human (); Scanner scan = new Scanner(System.in); //Call function int i1 = scan.nextInt();//Enter an int data in the console int i2 = scan.nextInt();//Enter an int data in the console double d = scan.nextDouble();//Enter a double data in the console String str = scan.next();//Enter a String data in the console
6, String splicer+
1. Meaning
+Both sides are numeric values. This symbol is an arithmetic operator
+There are strings on one or both sides. This symbol is a string splicer
2. Code
System.out.println(1+2+"abc"+"def"+1+2);//3abcdeef12 // 3 +"abc" + "def" +1+2 // "3abc" + "def" +1+2 // "3abcdef" +1+2 // "3abcdef1" +2 // "3abcdef12"
7, Expression
1. Meaning
5 + 6: arithmetic expression
5> 6: relational expression
True & false: logical expression
2. Knowledge point ternary operator / ternary operator
2.1 syntax
Data type variable = (expression)? Value 1: value 2;
2.2 understanding
The result of an expression must be of type boolean
true - assigns a value of 1 to the variable
false - assigns the value 2 to the variable
2.3 experiment
int num = (false)?10:20; System.out.println(num);
2.4 cases
2.4.1
Enter two numbers of type int in the console and output the maximum value
//Create scan object of Scanner class Scanner scan = new Scanner(System.in); //Enter two numbers System.out.println("Please enter the first number:"); int a = scan.nextInt(); System.out.println("Please enter the second number:"); int b = scan.nextInt(); //Judge size int max = (a>b)?a:b;//Judge whether a is greater than b. if it is greater than b, return a; otherwise, return b //Maximum output System.out.println("The maximum value is:" + max);
2.4.2
Enter two numbers of type int in the console and output the minimum value
//Create scan object of Scanner class Scanner scan = new Scanner(System.in); //Enter two numbers System.out.println("Please enter the first number:"); int a = scan.nextInt(); System.out.println("Please enter the second number:"); int b = scan.nextInt(); //Judge size int min = (a<b)?a:b;//Judge whether a is greater than b. if it is less than b, return a; otherwise, return b //Maximum output System.out.println("The minimum value is:" + min);
2.4.3
Enter three numbers of type int in the console and output the maximum value
Scanner scan = new Scanner(System.in); System.out.println("Please enter the first number:"); int a = scan.nextInt(); System.out.println("Please enter the second number:"); int b = scan.nextInt(); System.out.println("Please enter the third number:"); int c = scan.nextInt(); int max = (a>b)?a:b; max = (max>c)?max:c; System.out.println("The maximum value is:" + max);
2.4.4
Input three int type numbers on the console and output them from small to large
Scanner scan = new Scanner(System.in); System.out.println("Please enter the first number:"); int a = scan.nextInt(); System.out.println("Please enter the second number:"); int b = scan.nextInt(); System.out.println("Please enter the third number:"); int c = scan.nextInt(); //Get maximum int max = (a>b)?a:b; max = (max>c)?max:c; //Get minimum value int min = (a<b)?a:b; min = (min<c)?min:c; //Get intermediate value int mid = a+b+c-max-min; System.out.println(min + "<" + mid + "<" + max);
3. Go deep into the three item operator
3.1 extended interview question 1:
int a = 5; System.out.println((a<5)?10.9:9);//9.0
3.2 extended interview question 2:
char x = 'x';//'x' - ASCII - 120 int i = 10; System.out.println(false?i:x);//120
3.3 extended interview question 3:
char x = 'x';//'x' - ASCII - 120 System.out.println(false?100:x);//x System.out.println(false?65536:x);//120
3.4. Return value rule of ternary operator:
1. When values 1 and 2 are constants, data will be returned according to types with a wide range of values
2. If values 1 and 2 are variables, data will be returned according to types with a wide range of values
3. If value 1 is a constant and value 2 is a variable, is value 1 within the value range of the type to which value 2 belongs
In, the data is returned according to the value 2 type
If not, the data is returned according to the value 1 type
8, Bitwise operator
&And | or ^ XOR
< < move left
>>Shift right
>>>Unsigned bit shift right
1. Meaning
Convert decimal data into binary before operation
//&: in the same bit comparison, if the two are 1, the result is 1 byte b1 = 23;//0001,0111 byte b2 = 25;//0001,1001 //0001,0001 //23 byte - 0001,0111 //23 int - 0000,0000,0000,0000,0000,0000,0001,0111 //25 byte - 0001,1001 //25 int - 0000,0000,0000,0000,0000,0000,0001,1001 //17 int - 0000,0000,0000,0000,0000,0000,0001,0001 //17 byte - 0001,0001 byte result = (byte)(b1 & b2); System.out.println(result);//17 - 0001,0001 //|: if there is 1 between them, the result is 1 byte b1 = 23;//0001,0111 byte b2 = 25;//0001,1001 //0001,1111 System.out.println(b1 | b2);//31 - 0001,1111 //^: in the same bit comparison, the same is 0 and the difference is 1 byte b1 = 23;//0001,0111 byte b2 = 25;//0001,1001 //0000,1110 System.out.println(b1 ^ b2);//14 - 0000,1110 //&, |, ^: both before and after are numeric values, and the symbol is a bit operator //&, |, ^: both are boolean values, and the symbol is a logical operator //< <: move n bits to the left as a whole, and use n zeros to fill the bits byte b1 = 10;//0000,1010 //10 byte:0000,1010 //10 int:0000,0000,0000,0000,0000,0000,0000,1010 //40 int:0000,0000,0000,0000,0000,0000,0010,1000 System.out.println(b1 << 2); //>>: move n bits to the right as a whole, and use n highest bits to fill the bits byte b1 = 10;//0000,1010 //10 byte:0000,1010 //10 int:0000,0000,0000,0000,0000,0000,0000,1010 //2 :0000,0000,0000,0000,0000,0000,0000,0010 System.out.println(b1 >> 2); byte b2 = -10;//1111,0110 //-10 byte:1111,0110 //-10 int:1111,1111,1111,1111,1111,1111,1111,0110 //-3 :1111,1111,1111,1111,1111,1111,1111,1101 System.out.println(b2 >> 2); //>>>: move n bits to the right as a whole, and use n zeros to fill the bits byte b1 = 10;//0000,1010 //10 byte:0000,1010 //10 int:0000,0000,0000,0000,0000,0000,0000,1010 //2 :0000,0000,0000,0000,0000,0000,0000,0010 System.out.println(b1 >>> 2); byte b2 = -10;//1111,0110 //-10 byte:1111,0110 //-10 int:1111,1111,1111,1111,1111,1111,1111,0110 //1073741821:0011,1111,1111,1111,1111,1111,1111,1101 System.out.println(b2 >>> 2); //Note: > > and > > > if they are operands, the effect is the same
2. Classic interview questions
2.1 classic interview question 1
Calculate 2 * 8 in the most efficient way
System.out.println(2<<3);
2.2 classic interview questions 2
Describes the output of the following code
//-1 int:1111,1111,1111,1111,1111,1111,1111,1111 //(byte):1111,1111 //(char): 1111111111111111111111 -- char type is transformed upward, and 0 is used to fill the bit //(int) :0000,0000,0000,0000,1111,1111,1111,1111 System.out.println((int)(char)(byte)-1);//65535
9, Operator priority
experience
Don't remember, use parentheses flexibly
10, Escape character
1. Meaning
Character itself with special meaning
\n: Indicates a line break
": indicates a double quotation mark
': indicates a single quotation mark
\: indicates a slash
\t: Indicates horizontal tabulation
System.out.print("Education with conscience\n"); System.out.print("Education with conscience\n"); System.out.println("Mr. He said:\"Education with conscience\""); System.out.println("Mr. He said:\'Education with conscience\'"); System.out.println("Mr. He said:\\Education with conscience\\"); System.out.println("3*3=9\t3*4=12\t3*5=15"); System.out.println("4*3=12\t4*4=16\t4*5=20");
11, Constant
1. Meaning
An immutable quantity during program execution
2. Classification
1. Digital literal quantity (ps: 69, 100, 200)
2. Literal value constant (ps: "sanshang Youya", "Shentian Yongmei")
3. Final modified variable
final int i = 100;
System.out.println(i);
1. Meaning
Character itself with special meaning
\n: Indicates a line break
": indicates a double quotation mark
': indicates a single quotation mark
\: indicates a slash
\t: Indicates horizontal tabulation
System.out.print("Education with conscience\n"); System.out.print("Education with conscience\n"); System.out.println("Mr. He said:\"Education with conscience\""); System.out.println("Mr. He said:\'Education with conscience\'"); System.out.println("Mr. He said:\\Education with conscience\\"); System.out.println("3*3=9\t3*4=12\t3*5=15"); System.out.println("4*3=12\t4*4=16\t4*5=20");
11, Constant
1. Meaning
An immutable quantity during program execution
2. Classification
1. Digital literal quantity (ps: 69, 100, 200)
2. Literal value constant (ps: "sanshang Youya", "Shentian Yongmei")
3. Final modified variable
final int i = 100;
System.out.println(i);
Literal constants and final (final) decorated variables: stored in the constant pool and not destroyed until the end of the project