java 1l 表示长,1f 表示浮点,1d 表示双,字节呢?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15701795/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 20:34:14  来源:igfitidea点击:

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

javacastinginitializationbyte

提问by sp00m

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

1l 表示长,1f 表示浮点,1d 表示双,字节呢?

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

What's the equivalent for byte? Does it exist?

相当于byte什么?它存在吗?

回答by Jesper

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

不,没有后缀可以附加到数字文字以使其成为byte.

See 3.10 Literalsin the Java Language Specification.

请参阅Java 语言规范中的3.10 文字

回答by anubhava

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.

因为默认情况下,这些数字常量在 Java 中被视为 int。

回答by PSR

there is no suffix you can append a numeric literal

没有后缀可以附加数字文字

回答by Mark Rotteveel

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

字节没有这样的后缀,请参阅Java 语言规范部分 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.

注意(opt)表示它是可选的。因此,要分配您需要使用(byte) 1.