Is there a default type for numbers in Java

This

18

is known as an integer literal. There are all sorts of literals, floating point, String, character, etc.

In the following,

byte b = 3;

the literal 3 is an integer literal. It’s also a constant expression. And since Java can tell that 3 fits in a byte, it can safely apply a narrowing primitive conversion and store the result in a byte variable.

In this

int i = 3;
byte bb = i; //error!

the literal 3 is a constant expression, but the variable i is not. The compiler simply decides that i is not a constant expression and therefore doesn’t go out of its way to figure out its value, a conversion to byte may lose information (how to convert 12345 to a byte?) and should therefore not be allowed. You can override this behavior by making i a constant variable

final int i = 3;
byte bb = i; // no error!

or by specifying an explicit cast

int i = 3;
byte bb = (byte) i; // no error!

Leave a Comment