This article brings you relevant knowledge about java, which mainly introduces related issues about java operators, including arithmetic operators, relational operators, logical operators, etc. ,I hope everyone has to help.
## Recommended study: "java tutorial"
1. Arithmetic operatorsMeaning | |
---|---|
##Sum | |
Subtraction | ##* |
/ | |
% | |
– | |
increment by one before doing the assignment operation int x = 100;
int y = ++x;
步骤: ② ①
System.out.println(x); // 101
System.out.println(y); // 101
1.2 after the variable
do the assignment operation first, and then add 1 int m = 20;
int n = m++;
步骤: ① ②
System.out.println(n); // 20
System.out.println(m); // 21
1.3 Especially, in In print
int c = 90; System.out.println(c++); // 传,这个“传”在这里有一个隐形的赋值运算。90 // 把上面代码拆解开 //int temp = c++; //System.out.println(temp); int d = 80; System.out.println(++d); //81 // 拆解 //int temp2 = ++d; //System.out.println(temp2);2. --Operator (the example is the same as operator)2.1 --In front of the variable When – appears in the variable Before, you will
decrement yourself by one before doing the assignment operation2. --After the variable
will be done first, and then decremented by 1Note:
For operators: 1. It can appear before a variable or after a variable. 2. Regardless of whether it appears before or after the variable, in short, after the execution is completed, the value of the variable will definitely increase by 1.
2. Relational operators
##>= | greater than or equal to |
##< | Less than |
<= | Less than or equal to |
== | Equal to |
!= | is not equal to |
##Note |
Operator
& | Logical AND (can be translated as AND) | |
---|---|---|
│ | Logical OR (can be translated into or) | If one side is true, the result is true |
! | Logical negation (inversion) | !true = false, !false = true |
&& | short circuit and | Both sides is true, the result is true |
││ | short circuit or | if one side is true, the result is true |
1. Short-circuit and && | 1.1 What is the difference between short-circuit and && and logical AND&? |
int x = 10; int y = 11; System.out.println(x > y & x > y++); //false // 通过这个测试得出:x > y++ 这个表达式执行了。 System.out.println(y); // 12 //测试短路与&& int m = 10; int n = 11; // 使用短路与&&的时候,当左边的表达式为false的时候,右边的表达式不执行 // 这种现象被称为短路。 System.out.println(m > n && m > n++); System.out.println(n); // 11<h3>1.2 What is short circuit phenomenon? </h3> <p>The expression on the right is not executed. This phenomenon is called a short circuit phenomenon. <br><code>1.3 When to use && and when to use &? </code></p>In terms of efficiency, && is more efficient than &. <h3> Because of the logical AND &, no matter what the result of the first expression is, the second expression will definitely be executed. </h3> <p>In future development, short-circuit and && and logical AND still need to coexist at the same time. </p> In most cases, it is recommended to use short-circuit AND &&. Only when both the expression on the left and the expression on the right need to be executed, the logical AND& will be selected. <h3></h3>2. Short circuit or ||<p><br></p> is similar to short circuit<p><br></p> <pre class="brush:php;toolbar:false"> int x = 10; int y = 11; System.out.println(x < y | x > y++); //teur // 通过这个测试得出:x > y++ 这个表达式执行了。 System.out.println(y); // 12 //测试短路或|| int m = 10; int n = 11; // 使用短路或||的时候,当左边的表达式为true的时候,右边的表达式不执行 // 这种现象被称为短路。 System.out.println(m < n || m > n++); System.out.println(n); // 113. Summary
When using short-circuiting and &&, when the expression on the left is false
, the expression on the right is not executed. When the expression result is
true, the expression on the right side is not executed
## Note 4. Assignment operator
OperatorMeaning= | |
---|---|
= | Add and so on (add/append this number to the original one) |
-= | Subtraction equals (same reason) |
*= | Multiplication equals (same reason) |
/= | Division and equality (same principle) |
%= | Module and equality (same principle) |
注:除了第一个是赋值运算符,其他都死拓展赋值运算符!! 很重要的语法机制:使用 int m = 10; m += 10; 类似于 m = m + 1;------->注意是类似!!!! 实际不同: i = i + 10; 和 i += 10;一样吗? byte i = 10; i += 10;----->没报错 其实 x += 1 等同于:x = (byte)(x + 1); i = i + 10;---->错误: 不兼容的类型: 从int转换到byte可能会有损失 编译器检测到x + 1是int类型,int类型不可以直接赋值给byte类型的变量x! 详见Java类型转换的时候需要遵循的规则第六点 i += 190; // i = (byte)(i + 190); System.out.println(i); // 44 (当然会自动损失精度了。) 五、条件运算符1.语法格式:(三目运算符。)布尔表达式 ? 表达式1 : 表达式2 2.执行原理是什么?布尔表达式的结果为 好玩点: char a = true ? '男' : "女"; string a1 = true ? '男' : "女"; 以上两个都报错。 //下面可以 String s = ""; s += true ? '男' : "女"; 六、字符串连接运算符(+)1.+ 运算符在java语言中有两个作用。
2.什么时候求和?什么时候进行字符串的拼接呢?
3.一定要记住:字符串拼接完之后的结果还是一个字符串。int a = 100; int b = 200; // 这里的 + 两边都是数字,所以加法运算 int c = a + b; System.out.println(a + "+" + b + " = " + a + b);//100+200=100200 System.out.println(a + "+" + b + " = " + (a + b));//100+200=300 注:遵循“ 推荐学习:《java详细教程》 |
The above is the detailed content of Explain Java operators in detail (summary sharing). For more information, please follow other related articles on the PHP Chinese website!