English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

x in Java programming ++and x = x + 1Differences between

if x is not an int variable, then x ++Type conversion is handled automatically because x = x + 1needs to be converted. See the following example.

Example

public class Tester {
   public static void main(String args[]) {
      byte b = 2;
      //a type conversion must be performed
      //because1is an int while b is a byte variable
      b = (byte) (b + 1);
      System.out.println(b);
      byte b1 = 2;
      //is implicitly converted to type by the compiler
      b1++;
      System.out.println(b1);
   }
}

Output Result

3
3