运算符 | Java语言基础

运算符 不仅是 Java ,在众多编程语言中基本上都是相通的,学会迁移

Java中运算符可分为以下:算术运算符(基本+ - * /,增量+= -= *= %=,自增++/自减--)、关系运算符(< > == !=)、逻辑运算符(&& || ! & |)、位运算符(& | ^ ~ )、移位运算符(<< >> >>>)以及条件运算符等。

算数运算符

算数异常

做除法和取模的时候右操作数不能为0,否则会出现“除0异常”

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

这里 ArithmeticException 代表这里是一个算数异常的结果,后面 / by zero代表 “除0” ,第二行是定位错误在哪一行。

取模规则

public class Test {
    public static void main(String[] args) {
        int c = 7;
        int d = 3;
        System.out.println( c %  d); // 1
        System.out.println(-c %  d); //-1
        System.out.println( c % -d); // 1
        System.out.println(-c % -d); //-1
    }
}
  • Java 中 % 运算符的规则是:余数的符号与被除数相同。
  • 对于正数,余数就是通常的”除法余数”。
  • 对于负数,余数会按照规则调整,以确保余数的符号与被除数一致,且余数的绝对值小于除数的绝对值。

而且在Java语言中取模支持运算符两边均为浮点数,和C语言两边均为整数不同。

public class Test {
    public static void main(String[] args) {
        System.out.println(25.5%2.0); // 1.5
    }
}

自增运算符

如果num为一个已定义的整型变量,形如 num++++num 的表达式中 ++ 代表自增,同理,-- 代表自减。

如果单独使用这两个效果一样。都是表示为直接+1。而混合使用则分为两种:

  • 前置 ++ :先给变量+1,然后使用变量的值;
  • 后置 ++ :先使用变量原来的值,再表达式结束后给变量+1;
public class Test {
    public static void main(String[] args) {
        int a = 10;
        System.out.println(a++); // 11
        System.out.println(a);   // 12
    }
}
public class Test {
    public static void main(String[] args) {
        int a = 10;
        System.out.println(++a); // 12
        System.out.println(a);   // 12
    }
}

关系运算符与逻辑运算符

C语言中也有相通的《从猜数字程序来理解结构和随机数 – 3.1 Intro》

关系运算符有> < != == >= <=,返回 truefalse.

同样的,和C语言一样,形如 0<a<9 多次判断是不能连着写的(已定义整型变量a),要写成 a>0 && a<9 。因为原来的错误写法,如果判断的话,假设 a>0 结果确实是 true 了,但是后面判断就是 true<9 这样是无法比较的。

public class Test {
    public static void main(String[] args) {
        int a=10;
        System.out.println(a>10);  // false  正确写法
        //错误写法: System.out.println(1<a<11);
        System.out.println(a>1 && a<11); // true 正确写法
        System.out.println(a>10!=true); // true 正确的连续判断写法
    }
}

逻辑运算符有与&& (也有单&)或|| (也有单|)非! ,注意与和或操作两边的表达式是结果为boolean类型的表达式。

  • 与:有false出false,全true才true
  • 或:有true出true,全false才false
  • 非:即对立,!true==false !false==true

逻辑运算符的短路求值

短路求值就是提前判定,遵循着上述的“有x出x”。

  • 对于 && , 如果左侧表达式值为 false, 则总表达式结果⼀定是 false, ⽆需计算右侧表达式.
  • 对于 || , 如果左侧表达式值为 true, 则总表达式结果⼀定是 true, ⽆需计算右侧表达式.

所以这样的话,如果左侧表达式值能直接影响整个表达式结果的话,那么提前“短路”。这样即使右边表达式出错,也仍可以输出整个表达式的值。

public class Test {
    public static void main(String[] args) {
        System.out.println(10 > 11 && 10 / 0 == 0);  // 左侧为false,与逻辑中有false出false,所以提前短路
        System.out.println(10 < 11 && 10 / 0 == 0);  // 左侧为true,那么结果要取决于右侧,右侧表达式错误,所以抛出异常
    }
}

如果不希望短路求值,可以使用单 & 表示与和单 | 表示或。 但一般用得频率也比较少。

位运算符

Java 中数据存储的最⼩单位是字节,⽽数据操作的最⼩单位是位(bit)。

位运算符有四个 & | ~ ^,只有 ~ 是一元运算符。

位操作表⽰按⼆进制位运算,按位运算就是在按照⼆进制位的每⼀位依次进⾏计算。

按位与、按位或、按位异或

对于 ^ 异或来说:那就是相同为0,不同为1。(可以想象成“找不同”)

对于 ~ 按位取反操作来说,可以说是把二进制写出来后逐位取反。

public class Test {
    public static void main(String[] args) {
        System.out.println(0xa); // 10
        System.out.printf("%x\n",0xa);  //a,以十六进制输出
        System.out.printf("%32s\n",Integer.toBinaryString(0xa)); // 00000000000000000000000000001010
        System.out.println(~0xa);  // -11
        System.out.printf("%x\n",~0xa);  //fffffff5,以十六进制输出
        System.out.printf("%s\n",Integer.toBinaryString(~0xa));  // 11111111111111111111111111110101
    }
}

移位运算符

移位运算符有三个,<< 左移,>> 右移,>>> 无符号右移 (不存在无符号左移)。不常用,了解即可。对于一个正数来说,有如下规律

  • 左移:写出二进制并左移且右边补0,结果为 <原值>*2的<位数>幂次方
  • 右移:写出二进制并右移左边补符号位,结果为 <原值>/2的<位数>幂次方
  • 无符号右移:写出二进制并右移左边始终补0

条件运算符

格式 : <条件>?<真时执行>:<假时执行> 同C语言中的。

运算符优先级

不好记忆的话,那就加上括号调整优先级吧!

评论

24 条对“运算符 | Java语言基础”的回复

  1. 777xkgame 的头像

    777xkgame… sounds kinda fun, actually. If you’re bored and want to try something different, could be a good option. Have a blast! Check it out – 777xkgame

  2. 777becasino 的头像

    777becasino… Triple sevens, baby! Has to mean good fortune, right? Take a gamble, see if your luck’s in! Hope you get rich at 777becasino

  3. baji999app 的头像

    Just installed the baji999app! Seems promising, user friendly and lots of games. Anyone else tried it? Here’s the link: baji999app.

  4. pakwin111game 的头像

    Yo, pakwin111game is where it’s at for some seriously cool games. The gameplay rocks and the selection is huge. You’ll find something you dig, guaranteed. Let the games begin! pakwin111game

  5. mayapalace 的头像

    Alright alright, mayapalace… I’ve taken a spin there and the graphics are on point. It’s a solid place to hang out and kill some time, if you ask me. Good times guaranteed! Catch you there! mayapalace

  6. jq777gamedownload 的头像

    Yo, jq777gamedownload is legit! Downloaded some games and it was smooth sailing. Definitely worth checking out for some quick gaming fun! For real. jq777gamedownload

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注