1l for long, 1f for float, 1d for double, what about byte?
long l = 1l;
float f = 1f;
double d = 1d;
// byte b = 1?;
What's the equivalent for byte
? Does it exist?
1l for long, 1f for float, 1d for double, what about byte?
long l = 1l;
float f = 1f;
double d = 1d;
// byte b = 1?;
What's the equivalent for byte
? Does it exist?
No, there is no suffix you can append to a numeric literal to make it a byte
.
See 3.10 Literals in the Java Language Specification.
You need to cast to byte like this:
byte b = 1;
b = (byte) 5;
Since by default these numeric constant are treated as int in Java.
There is no such suffix for bytes, see the Java Language Specification section 3.10.1:
DecimalIntegerLiteral:
DecimalNumeral IntegerTypeSuffix(opt)
IntegerTypeSuffix: one of
l L
Note (opt)
signifies it is optional. So to assign you need to explicitly cast using (byte) 1
.