Type required for function calls using a short data type variable as a parameter

Whenever I use a byte or short data type as a parameter to a method, when I call the method, I need to explicitly pass the values ​​that I pass to these methods.

To better explain:

void foo(short x)
{}
void main() {foo((short)32);}

If I do not use short here, then a warning is generated.

method foo in class px cannot be applied to given types
required: byte
found: int

How can I get it better?

+3
source share
2 answers

No exit.

Java is not able to encode literals byteor short. Any literal of a number is a value int, and converting int to short without casting always generates a warning.

+4
source

int, int byte short , .

byte short, , .

+3

Source: https://habr.com/ru/post/1794480/


All Articles