Home > Java > javaTutorial > Explain Java operators in detail (summary sharing)

Explain Java operators in detail (summary sharing)

PHPz
Release: 2022-03-09 20:17:40
forward
4985 people have browsed it

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.

Explain Java operators in detail (summary sharing)

## Recommended study: "

java tutorial"

1. Arithmetic operators

OperatorMeaning-##*Product/quotient %Find the remainder (modulus) Add one by yourself–decrement by one1. Operator
##Sum
Subtraction
1.1 before the variable

When it appears before a variable, it will

increment by one before doing the assignment operation

	int x = 100;
	int y = ++x;
	步骤: ②	 ①
	System.out.println(x); // 101
	System.out.println(y); // 101
Copy after login
1.2 after the variable

When it appears after a variable, it will

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
Copy after login
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);
Copy after login
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

when- After appearing in a variable, the assignment operation

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

OperatorMeaning>greater than##>=greater than or equal to : The results of all relational operators are of Boolean type, either true or false, and cannot be other values.
##<Less than
<=Less than or equal to
==Equal to
!= is not equal to
##Note
3. Logical operators

Operator

Meaning

Result&Logical AND (can be translated as AND)If both sides are true, the result is true│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 andBoth sides is true, the result is true││ short circuit or if one side is true, the result is trueFirst of all, there is no difference in the results of these two operators, they are exactly the same. It's just that "short circuit and &&" will cause
1. Short-circuit and &&1.1 What is the difference between short-circuit and && and logical AND&?
short circuit

phenomenon.

	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); // 11
Copy after login
3. Summary

When using short-circuiting and &&, when the expression on the left is false

, the expression on the right is not executed.

  1. When using short-circuiting or ||, when the expression on the left is false When the expression result is true, the expression on the right side is not executed
  2. ## Note
  3. : logical operator Both sides are required to be of Boolean type, and the final operation result is also of Boolean type.

4. Assignment operator

Operator

MeaningAssignment
=
= 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 (当然会自动损失精度了。)
Copy after login

五、条件运算符

1.语法格式:(三目运算符。)

	布尔表达式 ? 表达式1 : 表达式2
Copy after login

2.执行原理是什么?

布尔表达式的结果为true时,表达式1的执行结果作为整个表达式的结果。
布尔表达式的结果为false时,表达式2的执行结果作为整个表达式的结果。

好玩点

	char a = true ? '男' : "女";
	string a1 = true ? '男' : "女";
	以上两个都报错。	
	//下面可以
	 String  s = "";
	 s += true ? '男' : "女";
Copy after login

六、字符串连接运算符(+)

1.+ 运算符在java语言中有两个作用。

  1. 作用1:求和
  2. 作用2:字符串拼接

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
Copy after login

:遵循“自左向右”的顺序依次执行。(除非额外添加了小括号,小括号的优先级高)

推荐学习:《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!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template