I thought numeric literal were by default int or doubles, depending on if have a . and numbers after the .
But I wrote a quick
test program as listed below
I understand the float float floatA = 5.5; failed to compile since 5.5 is a literal of type double and you are trying to assign this to a floag
What I am having problems with is byte byteA = 5;
5 is a literal of type int and this is being assigned to a byte and compiler should complain.
The compiler does not allow two byte values to be added and assigned to a byte since the result of the addition is an int
class literalTesting{
public static void main(
String[] arg){
byte byteA = 5; // allowed WHY I thought literal is an int and assigning int to byte
byte byteB = 10; // allowed
//byte bytec = byteA + byteB; // NOT ALLOWED byte + byte results in an int value trying to assign to byte
// ERROR incompatible types: possible lossy conversion from int to byte
short shortA = 5; // allowed
char charA = 5; // allowed
int intA = 5; // allowed
long longA = 5; // allowed
//float floatA = 5.5; // not allowed literal 5.5 returns a double - can not assign double to float without cast or F suffix
float floatA = 5.5F;
float floatB = (float)5.5;
}