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

13,065

Solution 1

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.

Solution 2

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.

Solution 3

there is no suffix you can append a numeric literal

Solution 4

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.

Share:
13,065
sp00m
Author by

sp00m

Updated on August 15, 2022

Comments

  • sp00m
    sp00m almost 2 years

    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?