1l for a long time, 1f for a float, 1d for two, but what about a byte?

1l for long, 1f for float, 1d for double, but what about a byte?

long l = 1l; float f = 1f; double d = 1d; // byte b = 1?; 

What is equivalent for byte ? He exists?

+6
source share
4 answers

No, there is no suffix that you can add to a numeric literal to make it byte .

See 3.10 Literals in the Java Language Specification.

+7
source

You need to send in byte as follows:

 byte b = 1; b = (byte) 5; 

Since by default these numeric constants are treated as int in Java.

+4
source

no suffix, you can add a numeric literal

+1
source

There is no such suffix for bytes, see Java 3.10.1 Language Specification :

 DecimalIntegerLiteral: DecimalNumeral IntegerTypeSuffix(opt) IntegerTypeSuffix: one of l L 

Note (opt) means that it is optional. Therefore, for the assignment, you need to explicitly use with (byte) 1 .

+1
source

All Articles