Preface
Basic Operators These are common in most programming languages, but take them out and write one.
Here, only some of the most commonly used operators are introduced, others are not commonly used to pick them up when they are used.
Basic Operators
1. Common binary operators +, -, *, /,%
1. Addition Operator (Sum Operator)+
The standard binary operator [^1.1], is also one of the most common operators. Other variables are usually used to save the calculated results for subsequent operations or for comparison purposes.
Usage: 10+8 (result 18, standard addition)
2. Subtraction Operator (Differential Operator)-
Standard binary operator [^1.1]; It is also one of the most common operators. Other variables are usually used to save the calculated results for subsequent operations or for comparison purposes.
Usage: 10-8 (result 2, standard subtraction)
3. Multiplication Operator*
Standard binary operator [^1.1]; It is also one of the most common operators. Other variables are usually used to save the calculated results for subsequent operations or for comparison purposes.
Usage: 10*8 (result 80, standard multiplication)
4. Division Operator/
Standard binary operator [^1.1]; It is also one of the most common operators. Other variables are usually used to save the calculated results for subsequent operations or for comparison purposes.
Usage: 10/8 (result is floating point type 1.25 or 1 if output is integer data)
5. Remainder Operator%
Standard binary operator [^1.1]; It is also one of the most common operators. Other variables are usually used to save the calculated results for subsequent operations or for comparison purposes.
Usage: 10%8 (result is 2, that is 2 after 10 divides 8)
2. Common unary operators++, -
Unary operators are not commonly used in practice and can shorten code to make it more concise.
Self-Incrementing Operator++.
The auto-increment for integer data variables, that is, one unit up, but the position of the auto-increment operator is critical, and an error in the operator position may even directly cause the program to run incorrectly.
Self-increasing before
As its name implies, auto-increment means auto-increment before invocation. The following:
public class App{ public static void main(String[] args){ int x = 0; //Initialize a variable System.out.println(x); //Output result is 0 System.out.println(++x); //Output result is 1 System.out.println(x); //Output result is 1 System.out.println(++x); //Output result is 2 System.out.println(++x); //Output result is 3 System.out.println(++x); //Output result is 4 } }
That is, the auto-increment operator precedes the variable name by the auto-increment operation, and the output is called after the auto-increment is completed.
Autologous growth after
That is, it is called before adding itself. The following:
public class App{ public static void main(String[] args){ int x = 0; //Initialize a variable System.out.println(x); //Output result is 0 System.out.println(x++); //Output result is 0 System.out.println(x); //Output result is 1 System.out.println(x++); //Output result is 1 System.out.println(x++); //Output result is 2 System.out.println(x++); //Output result is 3 System.out.println(x); //Output result is 4 } }
Most of the time, the use of arrays will increase, but generally you have to make a judgment based on the specific situation.
Self-Reducing Operator -
Same as self-adding, but from 1 unit up to 1 unit down.
Preceding Subtraction
As its name implies, auto-subtraction is the process of auto-subtraction before calling. The following:
public class App{ public static void main(String[] args){ int x = 5; //Initialize a variable System.out.println(x); //Output result is 5 System.out.println(--x); //Output result is 4 System.out.println(x); //Output result is 4 System.out.println(--x); //Output result is 3 System.out.println(--x); //Output result is 2 System.out.println(--x); //Output result is 1 } }
That is, the auto-subtraction operator appears before the variable name is a pre-auto-subtraction operation, after which the output is called.
After autosubtraction
That is, it is called before it is reduced. The following:
public class App{ public static void main(String[] args){ int x = 5; //Initialize a variable System.out.println(x); //Output result is 5 System.out.println(x--); //Output result is 5 System.out.println(x); //Output result is 4 System.out.println(x--); //Output result is 4 System.out.println(x--); //Output result is 3 System.out.println(x--); //Output result is 2 System.out.println(x); //Output result is 1 } }
3. Combinatorial Operators+=, -=, *=, /=
These operators are used more to shorten the code, so let's just look at two pieces of code.
public class App{ //Unoptimized descendant code by combination operator public static void main(String[] args){ int x = 0; //Initialize a variable x = x + 2; //composite operator System.out.println(x); //Output result is 2 x = x - 1; //composite operator System.out.println(x); //Output result is 1 x = x * 3; //composite operator System.out.println(x); //Output result is 3 x = x / 3; //composite operator System.out.println(x); //Output result is 1 } }
public class App{ //Optimizing the descendant code by combining operators //Note: Combinatorial operators are a whole and cannot have spaces in the middle //For example: x + = 2 will directly error // x += 2 Correct // X += 2 is also correct in theory, but it is really easy to be killed after writing to others... // x+ =2 Although spaces are generally skipped during compilation, overall operators cannot be split. Self-increasing operators cannot be separated by spaces either. public static void main(String[] args){ int x = 0; //Initialize a variable x += 2; //composite operator System.out.println(x); //Output result is 2 x -= 1; //composite operator System.out.println(x); //Output result is 1 x *= 3; //composite operator System.out.println(x); //Output result is 3 x /= 3; //composite operator System.out.println(x); //Output result is 1 } }
summary
These are the most common operators in normal programming and the basis of programming. Using the appropriate operators in the right place can shorten the code length.