Java 编程中的 x++ 和 x= x+1 有何不同


x++ 自动处理类型转换,而如果 x 不是一个 int 变量,则 x= x + 1 需要类型转换。请看下面的示例。

示例

 实时演示

public class Tester {
   public static void main(String args[]) {
      byte b = 2;
      //Type casting is required
      //as 1 is int and b is byte variable
      b = (byte) (b + 1);
      System.out.println(b);
      byte b1 = 2;
      //Implcit type casting by the compiler
      b1++;
      System.out.println(b1);
   }
}

输出内容

3
3

更新于: 26-Jun-2020

590 次浏览

开启你的 职业生涯

完成课程获得认证

开始学习
广告