You might think upto now that x+=10 is exactly same as x=x+10;

But there is still some difference,

Check this program

public class Fun {

public static void main(String[] args){

short x=1;

x+=1;//No compilation error

x=x+1;//Compilation error

//x=(short)(x+1);

}

}

See in the above x+=1 does not give any error but x=x+1 will give compilation error as can not convert from short to int.

So we have to do that manually.

So x+=1 first does the calculation and type case the result as of x and then assigns to x.

So x+=1 is equivalent to x=(type of x)(x+1)

## No comments:

## Post a Comment