preface
we learned the basic Operation rules Of course, our operators are indispensable in the process of operation, including "+", "-", "*", "/", which we are familiar with. And some special operators of Java language, such as self increasing, self decreasing, logical operators, etc. I won't be wordy here. Let's follow the author~~Tip: the following is the main content of this article. The following cases can be used for reference
1, Operator
1. Arithmetic operator
I believe everyone should know well about arithmetic operators, which is nothing more than adding "subtraction, multiplication, division and remainder"
Operator | describe | example |
---|---|---|
"+" | Addition ··· addition operator, indicating the addition of values on both sides | 10 + 20 equals 30 |
"-" | Subtraction... Left operand minus right operand | 30 - 20 equals 10 |
"*" | Multiplication ···· multiplication operator, which means that the values on both sides are multiplied | 10 * 20 equals 200 |
"/" | Division ··· left operand divides right operand | 20 / 10 equals 2 |
"%" | Remainder ··· the remainder of the left operand divided by the right operand | 20% 10 equals 0 |
1.1 cases
(1) Declare three int type variables with names (a, b, c);
(2) Use three numbers to practice addition, subtraction, multiplication, division and remainder
Note: "+" plus sign can be used for addition or string splicing
public static void main(String[] args) { //1. Create three variables named a, b and c int a = 10; int b = 20; int c = 25; //2. Addition, subtraction, multiplication, division and remainder operations //2.1. For addition operation, note that + here can be used for addition operation or splicing. System.out.println("+It's an addition operation:" + (a + b));//30 //2.2. Subtraction operation System.out.println("-It's subtraction:" + (b - a));//10 //2.3. Multiplication System.out.println("*It's multiplication:" + (a * b));//2000 //2.4. Division operation System.out.println("/It's division:" + (b / a));//2 /* * 2.5.The remainder operation is also called modulo * For example: * 20 / 10 = 2 ...... 0 The remainder is 0 * 10 / 20 = 0 ...... 10 The remainder is 10 * 25 / 10 = 2 ...... 5 The remainder is 5 */ System.out.println("%Remainder operation:" + (b % a));//0 System.out.println("%Remainder operation:" + (a % b));//10 System.out.println("%Remainder operation:" + (c % a));//5 }
Usage of '%':
used to judge whether it is an odd number or an even number, or whether it can be divisible
(1) If the integer value% 2 results in zero, the number must be even
(2) If the integer value% 2 results in 1, the number must be odd
(3) If the integer value%n results in 0, the number must be divisible by n
2. Self increasing and self decreasing operator
self increasing and self decreasing operator exists in high-level languages such as C/C++/C#/Java /. Its function is to add (or subtract) the value of a variable by 1 before or after the end of the operation (pre self increasing and self decreasing operator) or (post self increasing and self decreasing operator). We'll learn more later
be careful:
(1) "+ + a" is pre auto incremented, which will be automatically incremented in the output result first
(2) "-- a" pre self subtraction, self subtraction will be performed first, and then the result will be output
(3) "a + +" is set to auto increment, and the result will be output first and then auto increment
(4) "a --" is set to self subtraction, and the result will be output first and then self subtraction
Operator | describe | example |
---|---|---|
"++" | The value of auto increment ··· operation increases by 1 | 20 + + or + + 20 equals 21 |
"--" | The value of self subtraction ··· operation is reduced by 1 | 20 -- or -- 20 equals 19 |
2.1 case 1 (self increasing and self subtracting method of cognition)
(1) Declare four int type variables with names (d, e, f, g);
(2) Understanding pre self increment and post self increment
(3) Understanding pre subtraction and post subtraction
public static void main(String[] args) { //1. Declare four int variables with names of d, e, f and g int d = 10; int e = 10; int f = 10; int g = 10; //2. Recognize pre self increment and post self increment //2.1. Pre auto increment means that if the + + symbol appears in the front, it will auto increment first and then output the result System.out.println("Pre autoincrement:" + (++d));//The print result is: 11. First, d increases by 1 (10 + 1 = 11) and then print, so the result is 11 //2.2. Post auto increment means that if the + + symbol appears later, the result will be output first and then auto increment System.out.println("Post auto increment:" + (e++));//The print result is: 10. First output e,e is 10 at this time, and then execute + + auto increment 1, e is 11 at this time. We will print and view the result System.out.println(e);//The print result is: 11 //3. Understand pre self subtraction and post self subtraction //3.1. Leading self subtraction means that if the -- symbol appears in front, it will be subtracted first and then the result will be output System.out.println("Pre subtraction:" + (--f));//The print result is: 9, first f minus 1 (10-1 = 9), and then print, so the result is 9 //3.2. Post self subtraction means that if the -- symbol appears later, the result will be output first and then self subtraction System.out.println("Post auto increment:" + (g--));//The print result is: 10, first output g,g is 10 at this time, and then execute -- increase by 1, G is 9 at this time, and then print and view the result System.out.println(g);//The print result is: 9 }
2.2 case 2 (operation rules of self addition and self subtraction)
(1) Declare two variables of type int (h, i)
(2) Use multiplication and self increment operations, and declare that x, y receive the result value
be careful:
the priority of self increasing and self decreasing is higher than that of the four operations (arithmetic operations we will discuss earlier)
pre auto increment and auto subtraction (+ + A, - a): perform auto increment or auto decrement operation first, and then perform expression operation;
suffix self increment and self subtraction (a++,a --): first perform expression operation, and then perform self increment or self subtraction operation;
public static void main(String[] args) { //1. Declare two variables of type int (h, i) int h=5; int i=5; //2. Use multiplication and self increment operations, and declare that x and y receive the result value int x=2*++h;//Self increment first, and then expression operation int y=2*i++;//First perform expression operation, and then self increment //3. Print results System.out.println(x);//12 System.out.println(y);//10 }
2.3. Case 3 (self increasing and self decreasing comprehensive exercise)
Note (for self increase and self decrease, we should keep two points in mind):
(1) the symbol is in the front (front), which is changed before use;
(2) the symbol is after (after), which is used first and then changed;
public static void main(String[] args) { //1. Self increasing int a=1; System.out.println("Post auto increment:a=" + (a++));//Post auto increment, first operation and then change, so the output here is still 1 System.out.println("Post self increasing:a=" + a);//After the operation, it will change. At this time, a is 2 int b=1; System.out.println("Pre autoincrement:b=" + (++b));//Pre increment, change before operation. Therefore, the output here is the result 2 after self increment System.out.println("Pre self increasing:b=" + b);//There will be a self increasing change before the operation, so b is still 2 at this time //2. Self reduction int c=1; System.out.println("Post self subtraction:c=" + (c--));//After self subtraction, it operates first and then changes, so the output here is still 1 System.out.println("Post subtractive:c=" + c);//It will not change until the operation is completed, so c at this time is 0 int d=1; System.out.println("Pre subtraction:d=" + (--d));//Pre self subtraction, change before operation, so the output here is the result 0 after self subtraction System.out.println("Pre subtracted:d=" + d);//Self subtraction will occur before operation, so d is still 0 at this time }
2.3.1. Enhancement exercise (carry out the following exercises according to the above code)
public static void main(String[] args) { ...... Here is the code of case 3 //Enhancement exercise /** * Enhancement 1: self increment, consider the following calculation results. What is a now, what is b now, and what is the final result of the operation? */ System.out.println(++a+a+b++);//??? /** * Enhancement 2: self subtraction, consider the following operation results. What are the three c's? What's the final result? */ System.out.println(--c-c-c--);//??? }
2.3.2 enhancement results
//Enhancement exercise /** * Enhancement 1: self increment, consider the following calculation results. What is a now, what is b now, and what is the final result of the operation? */ System.out.println(a);//In case 3, the final value of a is 2 System.out.println(b);//In case 3, the final value of b is also 2 System.out.println(++a+a+b++);//??? System.out.println(b); /* * Enhanced analysis: * 1.In the exercise of case 3, the last value of a is 2, and the value of b is 2. Therefore, at this time, a=2 and b=2 before the first operation are enhanced * 2.In the process of operation, addition operation, so we look from left to right: * >The first is the pre increment + + a. the pre rule is to change before operation, so at this time, a is all 3, and the formula at this time is 3+3+b++ * >Then comes the post auto increment operation b + +. The post rule is to operate first and then change. Therefore, during the operation, the value of b is still 2, and the formula at this time is 3 + 3 + 2 * >The final result is 8 * Note that since b is a post operation, the final value of b after operation is 3; */ /** * Enhancement 2: self subtraction, consider the following operation results. What are the three c's? What's the final result? */ System.out.println(c);//In case 3, the final value of c is 0 System.out.println(--c-c-c--);//??? System.out.println(c); /* * Enhanced II analysis: * 1.In the exercise of case 3, the last value of c is 0, so before enhancing the two operation, c=0 * 2.Let's take a look at subtraction from left to right * >First, there is a pre subtraction -- c. The pre rule is to change before operation, so all c at this time is - 1, and the formula at this time is (- 1 minus - 1 minus - 1 --) * >Then there is a post subtraction operation c --, and the post rule is to operate first and then change, so the value of c is still - 1 during the operation, and the formula at this time is (- 1 minus - 1 minus - 1) * >Because we are negative numbers, in the process of operation, we can see that (- 1 minus - 1) is negative to positive. At this time, the operation formula becomes (- 1 + 1), so the final operation result is 0 * >Then (0 minus - 1) is still negative to positive, and the operation formula becomes (0 + 1) * >Finally, we get the operation result is 1 * Note: since c is the last post operation, the value of c after operation is - 2; */
3. Relational operator
operator | describe | example |
---|---|---|
"==" | Gets whether the values of two operands are equal. If they are equal, the condition is true | (100 = = 200) is false |
"!=" | Gets whether the values of two operands are equal. If the values are not equal, the condition is true | (100! = 200) is true |
">" | Check whether the value of the left operand is greater than the right operand. If so, the condition is true | (100 > 200) is false |
"<" | Check whether the value of the left operand is less than the right operand. If so, the condition is true | (100 < 200) true |
">=" | Check whether the value of the left operand is greater than or equal to the right operand. If so, the condition is true | (100 > = 200) is false |
"<=" | Check whether the value of the left operand is less than or equal to the right operand. If so, the condition is true | (100 < = 200) is false |
3.1 case 1
1. Declare two int type variables respectively;
2. Use relational operators to compare operands;
Note: false stands for false and true for true;
public static void main(String[] args) { //1. Declare two variables of type int int a = 10; int b = 20; //2. Use relational operators to compare operands /* * 2.1."==" -- be equal to * >If equal to, return true; if not equal to, return false */ System.out.println("a Is equal to b: " + (a == b));//The print result is: false, because 10 is not equal to 20, the result is false, and false is returned; /* * 2.2."!=" -- Not equal to * >!Indicates non, false if equal to, and true if not equal to */ System.out.println("a Whether unequal b: " + (a != b));//The print result is: true, because 10 is not equal to 20, the result is true and returns true; /* * 2.3.">" -- greater than * >If it is greater than, it returns true; if it is less than, it returns false */ System.out.println("a Greater than b: " + (a > b));//The print result is: false, because 10 is not greater than 20, the result is false, and false is returned /* * 2.4.">" -- less than * >If it is less than, it returns true; if it is greater than, it returns false */ System.out.println("a Less than b: " + (a < b));//The print result is: true. Because 10 is less than 20, the result is true and returns true /* * 2.5.">=" -- Greater than or equal to * >Greater than or equal to returns true, less than or false */ System.out.println("a Is it greater than or equal to b: " + (a >= b));//The print result is: false, because 10 is not greater than or equal to 20, the result is false, and false is returned /* * 2.5."<=" -- Greater than or equal to * >Less than or equal to returns true, greater than or false */ System.out.println("a Is it less than or equal to b: " + (a <= b));//The print result is: false, because 10 is less than or equal to 20, the result is false and returns true }
4. Logical operator
Operator | describe | example |
---|---|---|
"&&" | The condition is true if and only if both operands are true. | ((1 = = 1) & & (1 = = 2)) is false |
"||" | It is called a logical or operator. If any two operands are true, the condition is true. | ((1 = = 1) | (1 = = 2)) is true |
"!" | It is called a logical non operator and is used to reverse the logical state of operands. If the condition is true, the logical non operator gets false, If the condition is false, the logical non operator gets true. | ! ((1 = = 1) & & (1 = = 2)) is true |
Both logic and logic are short circuited (when the logical operator can get the result, it will not continue to operate the code on the right, so as to save space and certain performance) effect:
"& &" if the left operand (expression 1) is false, the right operand will not be executed
"|" if the left operand (expression 1) is true, the right operand will not be executed
4.1. Simple logic sorting
Logic and understanding: (I invite you to dinner & & I invite you to sing)
in fact, it's easy to understand. We can understand it as the "and" relationship in our life, such as eating and singing. I invite you to eat and sing. Literally, we can see that only when I am satisfied with all my relationships, I will not break my promise and the result will be true. On the contrary, as long as I am not satisfied with one, I will break my promise. The result is false.
Logical or understanding: (I invite you to dinner | I invite you to sing)
in fact, it's also easy to understand. We can understand it as the "or" relationship in our life, or for example, eating and singing, I invite you to dinner or I invite you to sing. We can see from the literal meaning that there are three possibilities: (1) I'm going to invite you to dinner. (2) I invite you to sing. (3) I invite you to dinner and sing. As long as I satisfy one of the relationships, I will not break my promise, and the result will be true. If I didn't invite you to dinner or take you to sing, I would take you to the library instead. If I fail to satisfy any relationship in logic or, I will break my promise and the result will be false.
Summary:
if judging A & & B, if A is false, whether B is false or true, the result is false.
add judgment A | B. If A is true, B will be true whether it is false or true.
4.2 case 1
(1) Declare three variables of type int
(2) Practice using logical operators
public static void main(String[] args) { //1. Declare three variables of type int int a = 50; int b = 40; int c = 30; //2. Practice using logical operators /* * 2.1.Logic and Practice * >All judgment conditions on both sides of "& &" must be met before the result is true, otherwise it is false */ System.out.println("a greater than b also b greater than c: " + (a > b && b > c));//The print result is: true, (50 > 40 & & 40 > 30). You can see that the judgments on both sides of "& &" are true, so the result is also true System.out.println("a greater than b also b less than c: " + (a > b && b < c));//The print result is: false, (50 > 40 & & 40 < 30). You can see that the judgment on the right side of "& &" is false, so the result is false /* * 2.2.Logic or practice * >The result is false only if all the judgment conditions on both sides of "|" are not true, otherwise it is true */ System.out.println("a greater than b perhaps b less than c: " + (a > b && b < c));//The print result is: true, (50 > 40 & & 40 < 30). You can see that the judgment on the left side of "|" is true. Due to the short-circuit effect of logic or, he will not judge the following conditions and will directly return true System.out.println("a less than b perhaps b less than c: " + (a < b && b < c));//The print result is: false, (50 < 40 & & 40 < 30). It can be seen that the judgment on both sides of "|" is false, so the result is false /* * 2.3."!"It means no, it means no, take the opposite,! true = false\!false = true */ System.out.println(!true);//The print result is false because we negate true System.out.println(!false);//The print result is true because we negate false /** * "&&"The judgment result on the left (50 > 40) should be true, and the judgment result on the right (40 < 30) should be false, * However, we negate it, and the right side becomes true. At this time, both the left and right sides are true. So the final result we get is true */ System.out.println(a > b && !(b < c));//The print result is true, and the formula is: true & &! false = true /** * "||"The judgment result on the left (50 < 40) should be false, but we reversed the result on the left, and the left is true at this time * Because of the short-circuit effect of "|", as long as there is a true in the front, the following judgment will not be made, so the result is true */ System.out.println(!(a < b) || b < c);//The permission result is true, and the judgment formula is:! false || false = true }
5. Assignment operator
Operator | describe | example |
---|---|---|
"=" | A simple assignment operator that assigns the value of the right operand to the left operand | C = A + B will assign the value obtained by A + B to C |
"+=" | The addition assignment operator, which adds the left and right operands and assigns them to the left operand | C += A is equivalent to C = C + A |
"-=" | Subtraction and assignment operator, which subtracts the left operand from the right operand and assigns the value to the left operand | C -= A is equivalent to C = C - A |
"*=" | The multiplication and assignment operator, which multiplies the left and right operands and assigns values to the left operand | C *= A is equivalent to C = C * A |
"/=" | The division and assignment operator, which divides the left and right operands and assigns them to the left operand | C /= A is equivalent to C = C / A |
5.1 case 1
(1) Declare three variables of type int
(2) Practice assignment operators
public static void main(String[] args) { //1. Declare three variables of type int int a = 10; int b = 20; int c = 0; //2. Practice assignment operators //2.1. "=" assignment operator c = a + b; System.out.println("a+b=" + c);//The print result is 30 //2.2. "+ =" assignment operator System.out.println("at present c=" + c); c += a; System.out.println("c+a=" + c);//The print result is 40 //2.3. "- =" assignment operator System.out.println("at present c=" + c); c -= a; System.out.println("c-a=" + c);//The print result is 30 //2.4. "* =" assignment operator System.out.println("at present c=" + c); c *= a; System.out.println("c*a=" + c);//The print result is 300 //2.5. "/ =" assignment operator System.out.println("at present c=" + c); c /= a; System.out.println("c/a=" + c);//The print result is 30 }
I think this should be understandable. In fact, it is the same as the previous addition, subtraction, multiplication and division, but it is abbreviated
6. Ternary operator
If the expression result is true, the value 1 is returned; otherwise, the value 2 is returned
Format: Boolean expression ? Expression 1 : Expression 2
6.1 case 1 (Xiao Yang looking for the classroom)
Xiao Yang is divided into classes, class A and class B (Class A and class B with scores above 200)
(1) Tell Xiao Yang to input the total score and receive the total score input by Xiao Ming.
(2) Judge the class assigned to Xiao Yang by the scores received
(2.1) if greater than or equal to 200 (true). He is a student of class A.
(2.2) if not greater than or equal to 200 (false). He is a student of class B.
Here we will use the Scanner class. If you don't understand it, you can read the content written earlier by the author Inputting personal information, calculating the area of a circle, and variable exchange
public static void main(String[] args) { //1. Tell Xiao Yang to input the total score and receive the total score input by Xiao Yang. //1.1. Tell Xiao Yang to enter the total score System.out.println("Please enter your total score:"); //1.2. Receive the total score entered by Xiao Yang int xiaoYang = new Scanner(System.in).nextInt(); //2. Judge whether Xiao Yang's total score is greater than or equal to 200. If so, he is a student of class A String SchoolClass = (xiaoYang >= 200) ? "A class" : "B class"; //3. Tell the class to which the sample is assigned System.out.println("Xiao Yang,Where is your class:" + SchoolClass); }
6.2 case 2 (Xiao Yang looking for the classroom - advanced version)
Xiao Yang has entered school and has to be divided into classes. Now he is divided into class S, class A, class B and class C
(1) Tell Xiao Yang to input the total score and receive the total score input by Xiao Ming.
(2) Judge the class assigned to Xiao Yang by the scores received
(2.1) if greater than or equal to 700. He is a student of class S.
(2.2) if greater than or equal to 400 but less than 700. He is a student of class A.
(2.3) if greater than or equal to 200 but less than 400. He is a student of class B.
(2.4) other students are class C.
I hope you readers can try to write it yourself. If you can't write it, look at the following code analysis logic. Exercise your ability to sort out logic. Big guy ignored ha ha~~
public static void main(String[] args) { //1. Tell Xiao Yang to input the total score and receive the total score input by Xiao Yang. //1.1. Tell Xiao Yang to enter the total score System.out.println("Please enter your total score:"); //1.2. Receive the total score entered by Xiao Yang int xiaoYang = new Scanner(System.in).nextInt(); //2. Judge whether Xiao Yang is a classmate of which class String SchoolClass = (xiaoYang >= 400) ? (xiaoYang >= 700 ? "S class" : "A class") : (xiaoYang >= 200 ? "B class" : "C class"); //3. Tell the class to which the sample is assigned System.out.println("Xiao Yang,Where is your class:" + SchoolClass); }
7. Use of plus sign (+)
the plus sign can be used for addition (both sides must be numeric values) or as a splice (as long as one of both sides is a string)
7.1 case 1 (use of test + number)
public static void main(String[] args) { //1. Both sides of the plus sign are numbers, and the plus sign will perform addition operation System.out.println(18 + 5);//The print result is 23, and the + sign between values indicates addition //2. Note that the bottom layer of char type is also a number System.out.println('A' + 1);//The print result is 66, and the number of a in char is 65. Therefore, it will not be used as splicing, but converted to 65 for addition between values //3. The plus sign can be used as a splicer to splice any data and string. As long as there is a string on both sides of the plus sign, it can be used as a splicer System.out.println("1" + 1);//Since 1 is wrapped in double quotation marks, the bottom layer will be considered as a string. At this time, the + sign is used as a splice //4. Output personal information String name = "Xiao Yang"; int age = 18; String salary = "20000"; //name is a young man, but he has a high salary. His monthly salary is salary System.out.println("full name:"+ name + "-age:" + age + "-monthly income:" + salary); }
8. Extended practice
Use the binocular expression to determine the maximum value entered by the user
(1) Remind the user to enter two values
(2) Receive two values of user input
(3) Use the three wood operator to get the value entered by the user
I believe you have experienced the above exercises. You should also have ideas here. You can try to write that sentence yourself. If you can't write it, look at the following code analysis logic. Exercise your ability to sort out logic. Big guy ignored ha ha~~
The author wrote here this time. If you have any questions or suggestions, you can tell me in the comment area. Thank you for your support!!!public static void main(String[] args) { //1. Remind the user to enter two values System.out.println("Get maximum,You are required to provide two values"); //2. Accept the two values entered by the keyboard Scanner scanner = new Scanner(System.in); int one = scanner.nextInt(); int two = scanner.nextInt(); //2. Use the ternary operator to obtain the maximum value in a and B //If a is greater than b, it is true and the maximum value is a. if a is not greater than b, it is false and the maximum value is b int max = one > two ? one : two; System.out.println("The maximum value is:"+max); }