java knowledge point operators

operator type

Assignment operator: Represented by the symbol "=", it is a binary operator. The left side must be a variable, the right side can be a constant, variable or expression, and when there are multiple assignment operators,right to leftrun

int a=10;//将10赋值给a
int b,c;
c=b=a+4;//将a和4的和赋值给b,再赋值给c

Arithmetic operators: multiplication ("*"), division ("/"), remainder ("%"'), addition ("+"), subtraction ("-")

public class Arith{
    
    							//创建类
	public static void main(String[] args){
    
    	//主方法
		float number1=45.56f;				//声明float型变量并赋值
		int number2=123;
		System.out.println("和为:"+(number1+number2));//输出两数的和
		/*其中"和为:"+的“+”为字符串连接符,可以将两个字符串连接成一个字符串,
		(number1+number2)加括号是因为“+”作为连接符和算数运算符同优先级,如果不加括号会将number1和number2当做字符串输出*/
		System.out.println("差为:"+(number1-number2))//输出两数的差
		System.out.println("积为:"+number1*number2)//输出两数的乘积
		System.out.println("商为:"+number1/number2)//输出两数的商
		//"*"和"/"不加括号是因为两者优先级比"+"要高
		}}

Increment and decrement operators: They are unary operators that can be placed before or after the operand, but the operand must be an integer or floating point type.

int a=1;
a++;//先使用a之后再把a+1
b=a++;//其中b=1,a=2
a--;//同理,先使用a的值,之后a-1
int a=1;
++a;//先运算,之后a+1
b=++a;//b=2,a=2
--a;//同理,先计算后,再使用a的值

Comparison operators:

public class Compare{
    
    
	public static void main(String[] args){
    
    
		int number1=4;
		int number2=5;
		if number1>number2//判断number1是否大于number2
		if number1<number2//判断number1是否小于number2
		if number1==number2//判断number1是否等于number2
		if number1<=number2//判断number1是否小于等于number2
		if number1=>number2//判断number1是否大于等于number2
		if number1!=number2//判断number1是否不等于number2
		//他们的结果返回值为true 或false 可用于if语句和while 语句的使用
		//注意区分赋值运算符"="与比较运算符"=="的区别
		} }

Logical operators: &(&&) (logical AND), || (logical OR), ! (logical NOT).
Difference: && and || are binary operators and run from left to right, while ! It is a unary operator and operates from right to left.

//&&:两边都成立时,才成立,即全真才为真
int a=2;
int b=3;
int c=4;
a<b&&b<c//因为a<b成立且b<c成立所以返回true
a>b&&a<c//因为a>b不成立,虽然a<c成立,但是仍然返回false
a>b&&a>c//都不成立,返回false
//||:两边有一个成立时,就成立(可认为||两边只有全部都不成立时才不成立)
a<b||b<c//因为a<b成立且b<c成立所以返回true
a>b||a<c//虽然a>b不成立,但是a<c成立,仍然返回true
a>b||a>c//都不成立,返回false
/*区分一下“短路”运算符和“非短路”运算符
&&:为“短路”运算符,在&两边时,当左边不成立时,直接停止计算,节省计算机判断次数
&:为“非短路”运算符,即使左边不成立,依然会继续运算右边表达式
*/

Bitwise operators: "bitwise AND", "bitwise OR", "bitwise negation", "bitwise XOR", "shift operation"
except "bitwise AND" and "bitwise OR", the others are only Can be used to handle integer operands.

Bit operations are operations that are entirely focused on bit aspects.
1. "Bitwise AND" operation: "&", if the corresponding bits of the two integer data a and b are both 1, the result is 1, otherwise it is 0, and if the precision of the two operands is different, thenThe precision of the result is the same as the high-precision operands`

byte a=3;
byte b=10;
a=00000011
b=00001010
c=a&b
c=00000010//因为是完全对位进行运算,只有2位置对应的位置a和b都为1,所以取1,其他都为0
//所以如果一个正数和一个负数进行“按位与”运算时,结果一定是正数,因为正数符号位为0,负数为1,“按位与”的话结果为0。

2. "Bitwise OR" operation: "|" If one of the corresponding bits of the two integer data a and b is 1, the result is 1, and it is 0 when all are 0, and if the precision of the two operands different, thenThe precision of the result is the same as the high-precision operands`

byte a=3;
byte b=10;
a=00000011
b=00001010
c=a&b
c=00001011//对应位置只要有一个为1,就取1
//所以如果一个正数和一个负数进行“按位或”运算时,结果一定是负数,因为正数符号位为0,负数为1,“按位或”的话结果为1。

3. "Bitwise negation": also known as "bitwise negation", "~" is a unary operator, which changes to 0 when it encounters 1, and changes to 1 when it encounters 0

4. "Bitwise XOR": "^", binary operator, when the two operands are both 0 or 1, the result is 0, otherwise it is 1, and if the precision of the two operands is different, thenThe precision of the result is the same as the high-precision operands`

5. Shift operation: <<: left shift, >>: right shift, >>>: unsigned right shift

//<<:左移
a=1
a=00000001
b=a<<2
b=00000100//将操作数的二进制数据,按照指定位数向做移动,末尾补0
b=1*2*2
/*一般情况下,一个数左移多少位相当于这个数乘以2的多少次方,不过有一个情况需要考虑,
就是当最左端的数由0以成1时,结果会变化*/
a=17
a=00100001
b=a<<2
b=10000100
b=-8//而不是17*2*2,因为这个数符号位变化,其中一个1变为符号位
//>>:右移,较复杂,当最高位是0时,右面用0填充,最高位为1时,最左端用1填充
a=-8
a=10001000
b=a>>2
b=11100010//因为最高位为1,所以填入1
b=-98
a=8
b>>2
b=00000010//因为最高位为0,填入0,一般情况下当操作数为正整数时,右移相当于右移几次就除以2的几次方
b=2//b=a/2/2
//>>>:无符号右移。无论最高位是0还是1,左侧都会填充0

Ternary operator: format (conditional expression? value 1: value 2)

//运算规则为:若条件式的值为true,则整个表达式取值1,否则取值2
boolean b=20<45?true:false;//当20<45为真时,取true ,为假时取false
//相当于if……else
boolean a;
if(20<45)//将20<45作为判断条件
	a=true//条件成立将true赋给a
else:
	a=false//条件不成立将false赋给a
	

operator precedencePriority decreases from top to bottom

Guess you like

Origin blog.csdn.net/m0_50760467/article/details/110438726