1

In Java there's a problem when we are using either byte or short data type.

byte y=19;
byte x=10;
y=y+x; //Compile Error:
System.out.println(y);

Here the output of this program.

error: incompatible types: possible lossy conversion from int to byte

But when we use x+=1, it won't generate any compile error.

byte y=19;
byte x=10;
y+=x; //No Errors !?    
System.out.println(y);

Output is 29

Is there a different between x=x+1 and x+=1using either byte or short data type?

Panduka Nandara
  • 504
  • 6
  • 14
  • maybe because in the first version it's trying to cast y into a byte but in the second it casts both x and y to ints – Meepo Dec 03 '17 at 02:49

0 Answers0