current position:Home>【Java】Operator

【Java】Operator

2022-09-23 10:18:10Desperately Azi

我不去想是否能够成功

既然选择了远方

便只顾风雨兼程

                                                                                                    —— 汪国真 


目录

1. 认识运算符 

1.1 认识运算符 

1.2 运算符的分类

2. 算术运算符

2.1 四则运算符 

 2.2 复合赋值运算符

2.3 自增 / 自减 运算符 

3.关系运算符 

4.逻辑运算符

4.1 逻辑与 &&

4.2 逻辑或 ||

4.3 逻辑非 ! 

4.4 短路求值

4.5 记忆口诀

5.位运算符 

5.1 按位与 &

5.2 按位或 |

5.3 按位取反 ~

5.4 按位异或 ^

6.移位运算

6.1 左移  <<

5.2 右移 >>

5.3 无符号右移 >>> 

6.条件运算符


1. 认识运算符 

1.1 认识运算符 

运算符:The symbols used to operate on operands are called operators(The most common operation is to perform mathematical operations),比如:+ - x  /  都是运算符.

public class Test {
    public static void main(String[] args) {
        int a = 1;// = :是赋值运算符
        int b = 2;
        int c = 1 + 2;// + :算术运算符
        System.out.println(c);
    }
}

上述代码中就是运算符,即:The symbols that operate on operands are operators,不同运算符操作的含义不同.

1.2 运算符的分类

2. 算术运算符

2.1 四则运算符 

四则运算符:+  -  *  /  %

四则运算符 是 双目运算符

补充知识:

  • 双目运算符:An operator with two operands is called a binary operator
  • 单目运算符:An operator with one operand is called a unary operator
  1. If the operands on both sides of the operator are integers, the result is an integer
  2. When the left and right operand types are inconsistent,Then the small type will be promoted to the large type

 The operands on both sides of the four operators can be变量也可以是常量

①  +   -   *   / 

public class Test {
    public static void main(String[] args) {
        int a = 1;
        int b = 2;
        System.out.println(a + b);
        System.out.println(a - b);
        System.out.println(a * b);
        System.out.println(a / b);
        System.out.println(1 + 2);
    }
}

a / b:int / int = int,如果我们想要得到小数,有两种方法

方法一:Coercion method

public class Test {
    public static void main(String[] args) {
        int a = 1;
        int b = 2;
        System.out.println((double) a / b);
    }
}

首先将 a 强制类型转换为 double 类型,然后除 b,当 b 看到 a double 类型,will be automatically upgraded todouble 类型 

方法二:Multiplying decimals 

public class Test {
    public static void main(String[] args) {
        int a = 1;
        int b = 2;
        System.out.println((a * 1.0) / b);
    }
}

首先我们知道 一个数 乘 1 等于这个数本身,a * 1.0 此时相当于 int * double = double ,a * 1.0的结果是 double 类型的,然后在除 b,就相当于 double / int = double

在做除法运算的时候右操作数不能为 0 

②  %

public class Test {
    public static void main(String[] args) {
        System.out.println(7 % 2);
        System.out.println(-7 % 2);
        System.out.println(7.5 % 2);
    }
}

运行结果 :

取余算法:

注:JavaThe left and right operands of the remainder operator can be floating point numbers,但是CThe left and right operands of the remainder operator in the language must be integers 

 在做取余运算的时候右操作数不能为 0 

 2.2 复合赋值运算符

复合赋值运算符:+=   -=    *=     /=      %=    <<=   等等

The compound assignment operator works:Assigns the result of the manipulation to the left operand.比如:(a = 1;)  a += 7;  相等于  a = a + 7;

public class Test {
    public static void main(String[] args) {
        int a = 1;
        a += 6;//等价于 a = a + 6 ,a = 7
        System.out.println(a);
        a -= 1;//等价于 a = a - 3 ,a = 6;
        System.out.println(a);
        a /= 2;//等价于 a = a / 2 ,a = 3;
        System.out.println(a);
        a *= 7;//等价于 a = a * 3 ,a = 21;
        System.out.println(a);
        a %= 5;//等价于 a = a % 3 ,a = 1;
        System.out.println(a);
        a += 7 + 1;//等价于 a = a + (7 + 1) ,a = 9;
        System.out.println(a);
    }
}

注:The left operand of the compound assignment operator must be变量,因为常量不允许被修改 

Compound assignment operators are used when different types are usedType conversion happens automatically,不需要我们自己强制转换 

public class Test {
    public static void main(String[] args) {
        int a = 1;
        a += 1.5;//等价于:a = (int)(a + 1.5)
        System.out.println(a);
    }
}

2.3 自增 / 自减 运算符 

自增 / 自减 运算符 :++   --

自增 / 自减 运算符的作用:on the value of the variable 加一 或 减一

Self-increment can be divided into 前置++后置++

  • 前置++:先加一,后运算
  • 后置++:先运算,后加一 

Self-reduction can be divided into 前置-- 后置--

  • 前置--:先减一,后运算
  • 后置--:先运算,后减一
public class Test {
    public static void main(String[] args) {
        int a = 9;
        System.out.println(++a);//先将a的值加一(a = 10),然后在打印.打印结果为:10
        System.out.println(a++);//先将a的值打印,Then add one(a = 11).打印结果为:10
        System.out.println(--a);//先将a的值减一(a = 10),然后在打印.打印结果为:10
        System.out.println(a--);//先将a的值打印,Then subtract one(a = 9).打印结果为:10
    }
}

虽然 ++a ,It doesn't seem to give a 赋值,但是 a The value in will still be +1

自增 / 自减 The operator does not appear to assign a value to the variable,But the value in the variable still changes 

注:只有变量才能使用自增/自减运算符,常量不能使用,因为常量不允许被修改 


3.关系运算符 

关系运算符:>  <   >=   <=    !=   == 

关系运算符的作用:用于判断是否为 true 或者是 false

public class Test {
    public static void main(String[] args) {
        int a = 10;
        int b = 20;
        System.out.println(a > b);//10 > 20 为假,打印false
        System.out.println(a >= b);//10 >= 20 为假,打印false
        System.out.println(a < b);//10 < 20 为真,打印true
        System.out.println(a <= b);//10 <= 20 为真,打印true
        System.out.println(a != b);//10 != 20 为真,打印true
        System.out.println(a == b);//10 == 20 为假,打印false
        System.out.println(1 > 2);//1 > 2为假,打印false
        System.out.println(1 < 2);//1 < 2为真,打印true
    }
}
  • Java 中 true 表示真,false 表示假
  • >=:表示大于或者等于 Only one of the two needs to be satisfiedtrue
  • !=:表示不等于
  • ==:JavaThe two equal signs in the middle indicate equality(一个等号表示赋值)
  • The operands on both sides of the relational operator can be variables or constants 

4.逻辑运算符

逻辑运算符:&&    ||     ! 

逻辑运算符的作用 :用于判断是否为 true 或者是 false

4.1 逻辑与 &&

&&: 双目运算符,The left and right sides are expressions,The result of the expression is both boolean 类型

&&语法规则:表达式1 && 表达式2,表达式1结果为true , 表达式2 结果为true,The result of the entire expression is true

public class Test {
    public static void main(String[] args) {
        int a = 10;
        int b = 20;
        int c = 30;
        System.out.println(a < b && c > b);//true && true,打印true
        System.out.println(a < b && c < b);//true && false,打印false
        System.out.println(a > b && c > b);//false && true,打印false
        System.out.println(a > b && c < b);//false && false,打印false
    }
}

注:boolean 类型只有两个值,一个是true ,还有一个是 false

4.2 逻辑或 ||

||:双目运算符,The left and right sides are expressions,The result of the expression is both boolean 类型 

|| 语法规则:表达式1 || 表达式2,表达式1结果 或者 表达式2结果 At least one is fortrue,The result of the entire expression is true 

public class Test {
    public static void main(String[] args) {
        int a = 10;
        int b = 20;
        int c = 30;
        System.out.println(a < b || c > b);//true || true,打印true
        System.out.println(a < b || c < b);//true || false,打印true
        System.out.println(a > b || c > b);//false || true,打印true
        System.out.println(a > b || c < b);//false || false,打印false
    }
}

4.3 逻辑非 ! 

!:目运算符,只有一个操作数.The result of the expression is both boolean 类型 

!语法规则:!(表达式),The result of the expression is true,Then the result of the entire expression is false;表达式的结果为 false,Then the result of the entire expression is false

public class Test {
    public static void main(String[] args) {
        int a = 10;
        int b = 20;
        System.out.println(!(a > b));//10 > 20,结果为false,Then after logical negation the result istrue
        System.out.println(!(a < b));//10 > 20,结果为true,Then after logical negation the result isfalse
    }
}

4.4 短路求值

对于 && 和 || ,All follow the short-circuit evaluation rules

① && 的短路求值 

public class Test {
    public static void main(String[] args) {
        int a = 10;
        int b = 20;
        System.out.println((a > b) && ((++a) > b));
        System.out.println(a);
    }
}

上述代码运行结果: 

代码解析: 

&& 两边操作数都为真,结果为真;If one result is false,则结果为假. 当执行 表达式1 found to be false,则整个表达式的结果为假.如果为真,Then continue to judge the next one 表达式2 的结果 ,表达式2 的结果为真,则整个表达式的结果为真,如果 表达式2 的结果为假,则整个表达式的结果为假

a > b ---->  10 > 20 ,表达式为假,则不会执行(++a)> b.So the result of the whole expression is false,并且 a 的值不变

② || 的短路求值 

public class Test {
    public static void main(String[] args) {
        int a = 10;
        int b = 20;
        System.out.println((a < b) || ((++a) < b));
        System.out.println(a);
    }
}

上述代码运行结果:

代码解析: 

|| At least one of the results of both operands is true,则整个表达式为真;都为假,则整个表达式为假 .当执行表达式1 when the result is true,则不会去执行 表达式2 ,整个表达式的结果就为真.当执行 表达式1 的时候,如果结果为假,则去执行 表达式2 ,表达式2 的结果为真,The result of the entire expression is true,要是 表达式2 的结果为假,则整个表达式的结果为假

4.5 记忆口诀

&&:All true

||:see the truth

!  : Either true or false,Not false is true


5.位运算符 

位运算符,见名知意 就可以知道是Operators that work with binary bits.

补充知识: 

Java 中数据存储的最小单位是字节,而数据操作的最小单位是比特位

1 字节 = 8 比特位

位操作表示 按二进制位运算. 计算机中都是使用二进制来表示数据的(二进制:01构成的序列), Bitwise operations are in accordance with 二进制位 Each bit is operated on in turn.

位运算符:&        |        ~        ^ 

  • &:按位与
  • | :按位或 
  • ~:按位取反
  • ^:按位异或

注:Bitwise operator operands can be variables or constants 

5.1 按位与 &

&:全一得一,Both bits are 1 ,则为 1 

public class Test {
    public static void main(String[] args) {
        int a = 10;
        int b = 20;
        System.out.println(a & b);
        System.out.println(7 & 6);
    }
}

上述代码运行结果:

代码分析: 

a = 10,b = 20,然后 a & b. 首先将 a b are converted to binary form,因为 a,b 是 int type so convert to 32 位二进制位:

a:00000000 00000000 00000000 00001010

b:00000000 00000000 00000000 00010100

Then do a bitwise AND,全一得一,则结果为:

00000000 00000000 00000000 00000000,结果就为 0

7 & 6, 7 和 6 默认都是 int 类型的,首先将 7 和 6 都转换为 32 位二进制位:

7:00000000 00000000 00000000 00000111

6:00000000 00000000 00000000 00000110 

Then do a bitwise AND,全一得一,则结果为:

00000000 00000000 00000000 00000110 ,就为 6

5.2 按位或 |

| :See you get one,Only one of the two bits is 1 ,则为 1 

public class Test {
    public static void main(String[] args) {
        int a = 10;
        int b = 20;
        System.out.println(a | b);
        System.out.println(7 | 6);
    }
}

上述代码运行结果: 

代码解析: 

a = 10,b = 20,然后 a | b. 首先将 a b are converted to binary form,因为 a,b 是 int type so convert to 32 位二进制位:

a:00000000 00000000 00000000 00001010

b:00000000 00000000 00000000 00010100

Then do a bitwise AND,See you get one,则结果为:

00000000 00000000 00000000 00011110,结果就为 30

7 & 6, 7 和 6 默认都是 int 类型的,首先将 7 和 6 都转换为 32 位二进制位:

7:00000000 00000000 00000000 00000111

6:00000000 00000000 00000000 00000110 

Then do a bitwise AND,See you get one,则结果为:

00000000 00000000 00000000 00000111 ,就为 7

5.3 按位取反 ~

~ : 1 变 0 , 0 变1

public class Test {
    public static void main(String[] args) {
        int a = 10;
        System.out.println(~a);
    }
}

上述代码运行结果:

 代码解析:

a = 10,然后 ~a . 首先将 a  转换成二进制的形式,因为 a 是 int type so convert to 32 位二进制位: 

a:00000000 00000000 00000000 00001010

Then get bitwise inverse,1 变 0 , 0 变 1,得:

11111111 11111111 11111111 11110101

Computers will only store the two's complement form,So the computer will think it's two's complement,When the user reads, the computer will automatically convert the complement code into the original code,Then read it out

There are two ways to convert the code to the original code

方法一:补码除了符号位,其他位按位取反,然后加1,的原码

方法二:补码减一得反码,The sign bit of the complement code remains unchanged and the other bits are reversed bit by bit to obtain the original code 

则 ~a 的结果就是 -11 

补充知识:

正数的原 反 补相同

负数的反码等于原码符号位不变,然后按位取反;The complement of a negative number is the complement of a negative number1 

5.4 按位异或 ^

^ :相同为 0,不同为 1 

public class Test {
    public static void main(String[] args) {
        int a = 10;
        int b = 20;
        System.out.println(a ^ b);
    }
}

上述代码运行结果 :

代码解析:

 a = 10,b = 20,然后 a ^ b. 首先将 a b are converted to binary form,因为 a,b 是 int type so convert to 32 位二进制位:

a:00000000 00000000 00000000 00001010

b:00000000 00000000 00000000 00010100

然后进行按位异或,相同为 0,不同为 1 则结果为:

00000000 00000000 00000000 00011110,结果就为 30


6.移位运算

移位运算符,见名知意 It must be an operation on binary bits again

移位运算符:<<         >>         >>>

  • <<:左移
  • >>:右移
  • >>>:无符号右移 

6.1 左移  <<

<< :最左侧位不要了, 最右侧补 0 

public class Test {
    public static void main(String[] args) {
        int a = 10;
        System.out.println(a << 1);
    }
}

上述代码运行结果: 

代码解析: 

a = 10,然后 a << 1 . 首先将 a  转换成二进制的形式,因为 a 是 int type so convert to 32 位二进制位: 

a:00000000 00000000 00000000 00001010

然后最左侧位不要了, 最右侧补 0 

00000000 00000000 00000000 00010100

Converting binary bits to decimal is just that 20 

注:

  • 向左移位时,丢弃的是符号位,因此正数左移可能会变成负数 
  • 左移 1 位, 相当于原数字 * 2. 左移 N 位, 相当于原数字 * 2 的N次方.

5.2 右移 >>

 >>:最右侧位不要了, 最左侧补符号位(正数补0, 负数补1)

public class Test {
    public static void main(String[] args) {
        int a = 10;
        System.out.println(a >> 1);
    }
}

上述代码运行结果: 

代码解析: 

a = 10,然后 a >> 1 . 首先将  a  转换成二进制的形式,因为 a 是 int type so convert to 32 位二进制位: 

a:00000000 00000000 00000000 00001010

然后No more right side, Left positive complement0, 负数补1

00000000 00000000 00000000 00001010

Converting binary bits to decimal is just that 20 

注:右移 1 位, 相当于原数字 / 2. 右移 N 位, 相当于原数字 / 2 的N次方. 

5.3 无符号右移 >>> 

>>>:最右侧位不要了, 最左侧补 0 

public class Test {
    public static void main(String[] args) {
        int a = -10;
        System.out.println(a >>> 1);
    }
}

上述代码运行结果:  

代码解析: 

a = -10,然后 a >>> 1 . 首先将  a  转换成二进制的形式,因为 a 是 int type so convert to 32 位二进制位: 

a:10000000 00000000 00000000 00001010

如果是负数,Then it needs to be converted to complement and then stored in the computer for operation 

原码:10000000 00000000 00000000 00001010 

反码:11111111 11111111 11111111 11110101 

补码:11111111 11111111 11111111 11110110

Then unsigned left shift: 01111111 11111111 11111111 11111011

结果就为:2147483643


6.条件运算符

There is only one conditional operator:?: 

条件运算符的格式:表达式1 ? 表达式2 : 表达式3

表达式1 的结果必须是 boolean 类型 

表达式1 的值为 true 时, 整个表达式的值为 表达式2 的值; 当 表达式1 的值为 false 时, 整个表达式的值为 表达式3 的值.

条件运算符也是 Java 中唯一的一个 三目运算符, 是条件判断语句的简化写法. 

public class Test {
    public static void main(String[] args) {
        int a = 10;
        int b = 20;
        int c = (a > b) ? a : b;
        System.out.println(c);
    }
}

 上述代码运行结果:

 代码解析:

如果 a > b 为 true ,则把 a 的值赋值给 c,如果 a > b 为 false ,则把 b 的值赋值给 c 

copyright notice
author[Desperately Azi],Please bring the original link to reprint, thank you.
https://en.cdmana.com/2022/266/202209231011089095.html

Random recommended