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

我们两清 提交于 2019-12-22 05:33:34

问题


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?


回答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.




回答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.




回答3:


there is no suffix you can append a numeric literal




回答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.



来源:https://stackoverflow.com/questions/15701795/1l-for-long-1f-for-float-1d-for-double-what-about-byte

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!