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 次瀏覽

開啟你的 職業生涯

完成課程獲得認證

開始學習
廣告