I can't add two short numbers when they are in a loop, but when they outside loop they adds well. Byte type behaves the same. But with integers everything works well. Why does this happening? Java, version 18.
Here is an example:
short a = 5;
short b = 6;
short c = 0;
int i = 0;
while(i < 10) {
c = a + b;
i++;
}
When I trying to compile this, it's give me error: incompatible types, possible lossy conversion from int to short.
This also do not work:
int i = 0;
while(i < 10) {
short a = 5;
short b = 6;
short c = 0;
c = a + b;
i++;
}
But if I do this(without loop):
short a = 5;
short b = 6;
short c = 0;
c = a + b;
it works well.