Java Operators

本文深入探讨Java中的操作符,包括优先级、前置与后置递增的区别、逻辑运算符的短路特性及位移运算符在不同数据类型上的表现。通过实例代码,帮助读者理解并掌握Java操作符的使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文简述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上测试通过。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值