Java Operators

本文简述Java中操作符的一些知识点,属于个人学习总结,能力有限,还望各位大神多多指点。

Precedence

优先级由高到低排序。

OperatorsPrecedence
postfixexpr++
expr–
unary++expr
–expr
+expr
-expr
~
!
multiplicative*
/
%
additive+
-
shift<<
>>
>>>
relational<
>
<=
>=
instanceof
equality==
!=
bitwise AND&
bitwise exclusive OR^
bitwise inclusive OR|
logical AND&&
logical OR||
ternary? :
assignment=
+=
-=
*=
/=
%=
&=
^=
|=
<<=
>>=
>>>=

虽然定义了操作符之间的优先级,但是还是要加上括号以提高可读性。

Notes

一些需要注意的地方。

Difference between expr++ and ++expr

The same to expr-- and --expr

public class Test {

    public static void main(String[] args) {
        int i = 0;
        // compare first, then increase
        if (i++ > 0) {
            System.out.println("true branch: " + i);
        } else {
            // go into this branch
            // false branch: 1
            System.out.println("false branch: " + i);
        }

        int j = 0;
        // increase first, then compare
        if (++j > 0) {
            // go into this branch
            // true branch: 1
            System.out.println("true branch: " + j);
        } else {
            System.out.println("false branch: " + j);
        }
    }

}

Difference between && and & in case of condition check

The same to || and |

public class Test {

    public static void main(String[] args) {
        int i = 0;
        int j = 0;
        // the whole conditions check is false if the first condition is false, then the following conditions don't need be checked.
        // so, j++ won't be executed actually.
        // this is called short circuit.
        if (i++ > 0 && j++ > 0) {
            System.out.println("true branch: i = " + i + ", j = " + j);
        } else {
            // go into this branch
            // false branch: i = 1, j = 0
            System.out.println("false branch: i = " + i + ", j = " + j);
        }

        int m = 0;
        int n = 0;
        // all conditions will be evaluated.
        if (m++ > 0 & n++ > 0) {
            System.out.println("true branch: m = " + m + ", n = " + n);
        } else {
            // go into this branch
            // false branch: m = 1, n = 1
            System.out.println("false branch: m = " + m + ", n = " + n);
        }
    }

}

Don’t use bitwise move on byte, short, and char

public class Test {

    public static void main(String[] args) {
        int i = 111;
        // 00000000000000000000000001101111
        printInt(i);
        // 00000000000000000000000000011011
        printInt(i >> 2);
        // 00000000000000000000000110111100
        printInt(i << 2);
        // 00000000000000000000000000011011
        printInt(i >>> 2);
        System.out.println();

        int j = -111;
        // 11111111111111111111111110010001
        printInt(j);
        // 11111111111111111111111111100100
        printInt(j >> 2);
        // 11111111111111111111111001000100
        printInt(j << 2);
        // 00111111111111111111111111100100
        printInt(j >>> 2);
        System.out.println();

        byte m = 111;
        // 00000000000000000000000001101111
        printInt(m);
        // 00000000000000000000000000011011
        printInt(m >> 2);
        // 00000000000000000000000110111100
        printInt(m << 2);
        // 00000000000000000000000000011011
        printInt(m >>> 2);
        System.out.println();

        byte n = -111;
        // 11111111111111111111111110010001
        printInt(n);
        // 11111111111111111111111111100100
        printInt(n >> 2);
        // 11111111111111111111111001000100
        printInt(n << 2);
        // 00111111111111111111111111100100
        printInt(n >>> 2);
        
        // pay more attention to the last sample
        // char, byte, and short literals are converted to int before any calculations.
        // so when work on bitwise move, it may not as you expected.
        // in this sample, binary code is 10010001 for byte
        // after >>> 2, expected binary code is 00100100
        // actual binary code is 00111111111111111111111111100100
        // someone suggests to cut the last 8 bits, i.e. 11100100
        // it's clearly that they are different
        
    }

    private static void printInt(int i) {
        String s = Integer.toBinaryString(i);
        System.out.println("0".repeat(32 - s.length()) + s);
    }
}

备注

本文所有代码在JDK 11上测试通过。

### 回答1: 以下是一个简单的 Java 测试布尔运算符的代码: ``` public class BooleanOperatorsTest { public static void main(String[] args) { boolean a = true; boolean b = false; System.out.println("a && b = " + (a && b)); System.out.println("a || b = " + (a || b)); System.out.println("!(a && b) = " + !(a && b)); } } ``` 输出结果为: ``` a && b = false a || b = true !(a && b) = true ``` 这个程序演示了 Java 的三个布尔运算符:逻辑与(&&)、逻辑或(||)和逻辑非(!)。 ### 回答2: 下面是一个简单的Java代码示例,演示了 boolean 运算符的用法: ```java public class BooleanOperatorsExample { public static void main(String[] args) { boolean a = true; boolean b = false; // 逻辑与运算符(&&) boolean result1 = a && b; System.out.println("逻辑与运算结果: " + result1); // 逻辑或运算符(||) boolean result2 = a || b; System.out.println("逻辑或运算结果: " + result2); // 逻辑非运算符(!) boolean result3 = !a; System.out.println("逻辑非运算结果: " + result3); // 逻辑异或运算符(^) boolean result4 = a ^ b; System.out.println("逻辑异或运算结果: " + result4); } } ``` 在这个程序,我们定义了两个 boolean 类型的变量 `a` 和 `b`,分别赋值为 `true` 和 `false`。然后,我们使用不同的 boolean 运算符来演示不同的操作。 - 逻辑与运算符(&&):如果两个操作数都为 true,则结果为 true,否则为 false。 - 逻辑或运算符(||):如果两个操作数有任意一个为 true,则结果为 true,否则为 false。 - 逻辑非运算符(!):用于取反,如果操作数为 true,则结果为 false,如果操作数为 false,则结果为 true。 - 逻辑异或运算符(^):如果两个操作数有一个为 true,另一个为 false,则结果为 true,否则为 false。 以上代码演示了这些运算符的用法,并打印出了相应的结果。 ### 回答3: Java的布尔运算符可以用于判断条件的真假,如果给定的条件成立,则表达式的值为真(true),否则为假(false)。 以下是一个简单的Java代码示例,展示了布尔运算符的使用: ```java public class BooleanOperators { public static void main(String[] args) { int num1 = 5; int num2 = 10; // 使用逻辑运算符判断条件是否成立 boolean isEqual = (num1 == num2); boolean isGreaterThan = (num1 > num2); boolean isLessThan = (num1 < num2); boolean isNotEqual = (num1 != num2); // 输出结果 System.out.println("Is equal: " + isEqual); System.out.println("Is greater than: " + isGreaterThan); System.out.println("Is less than: " + isLessThan); System.out.println("Is not equal: " + isNotEqual); } } ``` 在这个例子,我们定义了两个整数变量num1和num2,然后使用布尔运算符来判断不同的条件。 - isEqual变量使用双等号(==)运算符判断num1是否等于num2。 - isGreaterThan变量使用大于号(>)运算符判断num1是否大于num2。 - isLessThan变量使用小于号(<)运算符判断num1是否小于num2。 - isNotEqual变量使用不等号(!=)运算符判断num1是否不等于num2。 最后,我们将判断结果输出到控制台。运行这段代码,输出的结果将显示每个条件的真假情况。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值