java Java中的整数到字节转换
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7369493/
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
Integer to byte casting in Java
提问by xyz
In Java we can do
在Java中我们可以做
byte b = 5;
But why can't we pass same argument to a function which accepts byte
但是为什么我们不能将相同的参数传递给一个接受的函数 byte
myObject.testByte(5);
public void testByte (byte b)
{
System.out.println("Its byte");
}
It gives following error
它给出以下错误
The method testByte(byte) in the type Apple is not applicable for the arguments (int)
PS: May be a silly question, I think I need to revise my basics again.
PS:可能是一个愚蠢的问题,我想我需要再次修改我的基础知识。
Thanks.
谢谢。
回答by Amber
Hard-coded initializer values are somewhat special in Java - they're assumed to have a coercion to the type of the variable you're initializing. Essentially, that first bit of code effectively looks like this:
硬编码的初始化值在 Java 中有些特殊 - 它们被假定为对您正在初始化的变量类型进行强制转换。从本质上讲,第一段代码实际上如下所示:
byte b = (byte) 5;
If you did this...
如果你这样做...
myObject.testByte((byte) 5);
...you wouldn't get that error, but if you don't do that, then the 5
is created by default as an int
, and not automatically coerced.
...你不会得到那个错误,但如果你不这样做,那么5
默认情况下将创建为int
,而不是自动强制。
回答by Bohemian
The reason is that when you are narrowinga primitive, you must explicitly make a cast - so you acknowledge a possible loss of data.
原因是当您缩小原语范围时,您必须明确进行强制转换 - 因此您承认可能会丢失数据。
To illustrate, when casting 5
there is no loss because the value is within the -128...127
byte value range, but consider a larger int
value, say 300
- if you cast to byte, you must throw away some bits to make it fit into 8 bits.
举例说明,当转换时5
没有损失,因为该值在-128...127
字节值范围内,但考虑更大的int
值,例如300
- 如果转换为字节,则必须丢弃一些位以使其适合 8 位。
The topic is covered in full here.
此处详细介绍了该主题。
回答by Stuart Cook
Normally, converting an int
to a byte
without an explicit cast is not allowed.
通常,不允许int
在byte
没有显式强制转换的情况下将 an 转换为 a 。
However, if the conversion is part of an assignment, and the value is a statically-known constant that will fit in the destination type, the compiler will perform the conversion automatically.
但是,如果转换是赋值的一部分,并且值是适合目标类型的静态已知常量,则编译器将自动执行转换。
This special behaviour is described in section 5.2 of the JLS. It is a special case that only applies to assignment; it does not apply to conversions in other contexts.
JLS 的第 5.2 节中描述了这种特殊行为。这是一种特殊情况,仅适用于赋值;它不适用于其他上下文中的转换。
Now that I think about it, the lack of auto-narrowing for arguments is probably to avoid issues with overload resolution. If I have methods #foo(short)
and #foo(char)
, it's not clear which one foo(65)
should call. You could have special rules to get around this, but it's easier to just require the caller to be explicit in all cases.
现在我想起来,缺乏参数的自动缩小可能是为了避免重载解析问题。如果我有方法#foo(short)
and #foo(char)
,不清楚foo(65)
应该调用哪个。你可以有特殊的规则来解决这个问题,但只要求调用者在所有情况下都是明确的更容易。
回答by jmnwong
You must cast your argument 5
to type byte
in your method testByte
. Java looks specifically at the argument type.
您必须将参数强制转换5
为键入byte
您的方法testByte
。Java 特别关注参数类型。
Change it to:
将其更改为:
myObject.testByte( (byte) 5);
回答by Mohammad Faisal
Integer literals are by default int
in Java. And in myObject.testByte(5);
5
is an integer literal which will be treated as int
.
整数文字int
在 Java中是默认的。而 inmyObject.testByte(5);
5
是一个整数文字,将被视为int
.
As all you know Java is strictly types language, so it will not allow to assign an int
to a byte
. All you need to have explicit type-casting.
众所周知,Java 是严格的类型语言,因此它不允许将 an 分配int
给 a byte
。您只需要进行显式类型转换即可。