为什么允许从int到byte的隐式缩小转换,而不是从long到int? [英] Why is an implicit narrowing conversion allowed from int to byte but not from long to int?

查看:172
本文介绍了为什么允许从int到byte的隐式缩小转换,而不是从long到int?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下代码片段:

// automatic casting works for int to byte conversion as integer literal 127
// is in the range for byte
byte b1 = 127; //OK

// automatic casting doesn't work for long to int conversion 
// even if long literal is in the range of int.
int i5 = 100L; // NOT OK - compilation error

这种行为有什么解释吗?

Is there any explanation for such behavior?

为什么在int到byte的情况下不需要显式转换,但需要long到int?

Why is explicit conversion not needed in the case of int to byte, but needed for long to int?

Java如何将int转换为字节?问题是不同的。当int值超出范围时,它是关于int到byte的隐式转换的问题。

The How does Java convert int into byte? question is different. It is about an issue in implicit conversion of int to byte when the int value is out of range.

推荐答案

Java编译器通常会隐式地接受扩展转换(例如 byte int ),因为没有信息丢失( int 的范围大于 byte 的范围。)

Widening conversions (eg. byte to int) are generally accepted implicitly by the Java compiler, as there's no loss of information (the range of int is greater than that of byte).

缩小转化次数(例如 int ,如您的情况)可能导致信息丢失,因此通常需要明确投放。

Narrowing conversions (eg. long to int, as in your case) can cause a loss of information, so are generally required to be explicitly casted.

参见这个类似的问题,以及这个 Java语言规范的相关部分

See this similar question, and this. A relevant piece of the Java Language Specification:


当表达式的值被赋值(第15.26节)给变量时,就会发生赋值转换:表达式必须转换为变量的类型。

Assignment conversion occurs when the value of an expression is assigned (§15.26) to a variable: the type of the expression must be converted to the type of the variable.

...

此外,如果表达式是一个类型为byte,short,char或int的常量表达式(第15.28节):

In addition, if the expression is a constant expression (§15.28) of type byte, short, char, or int:


  • 如果变量的类型是byte,short或char,则可以使用缩小的原始转换,并且常量表达式的值可以在变量的类型中表示。

如果变量的类型是:

    $ b,则可以使用缩小的原始转换,然后进行装箱转换。 $ b
  • 字节和常量表达式的值是rep类型字节中的resentable

  • Byte and the value of the constant expression is representable in the type byte

简短,常量表达式的值可以在short类型中表示。

Short and the value of the constant expression is representable in the type short.

字符和常量表达式的值可以在char类型中表示。

Character and the value of the constant expression is representable in the type char.

(强调我的)

由于我们正在使用数字文字,因此我们特别处理常量表达式这一事实引发了一些混淆。以上规范也需要仔细阅读。

Some confusion stems from the fact that we're dealing in particular with constant expressions, as we're using numeric literals. The above spec also needs some careful reading.

要清除一些内容并直接回答OP的一些查询:

To clear some things up and directly answer some of the OP's queries:


  1. 为什么一种缩小而不是另一种缩小支持隐式缩小?

IE浏览器。为什么 byte b1 = 127 隐式工作,而 int i5 = 100L 不是?

Ie. why does byte b1 = 127 work implicitly, while int i5 = 100L not?

byte b1 = 127 执行隐式转换(参见上面引用中的粗体文本),常量表达式的值可表示在类型字节。也就是说, 127 可由字节表示,因此转换是隐式的。如果您尝试 byte b1 = 128 ,您将收到有关不兼容类型的错误,因为 128 无法表示按字节。我们允许隐式转换在所有的唯一原因是因为我们使用常量表达式

byte b1 = 127 performs an implicit conversion as (cf. the bold text in the above quote), "the value of the constant expression is representable in the type byte". That is, 127 is representable by a byte, so the conversion is implicit. If you try byte b1 = 128, you'll get an error about incompatible types, as 128 isn't representable by byte. The only reason we're allowed an implicit cast here at all is because we're using a constant expression.

我们在 int i5 = 100L 中没有隐式转换(即使100在 int )因为它只是没有在允许的隐式转换中列出(变量的类型, int ,不是 byte 之一> , char )。

We don't get an implicit conversion in int i5 = 100L (even though 100 is in the range of int) as that's simply not listed in the allowed implicit conversions (the variable's type, int, is not one of byte, short, or char).

我们还不要在字节a = 0L 中获得隐式转换,这次由于常量表达式的类型为 long ,不是类型字节字符 int

We also don't get an implicit conversion in byte a = 0L, this time as the constant expression is of type long, not of type byte, short, char, or int.


  1. 普通程序员如何知道隐含允许哪种缩小转换?

只有当您指定常量expressi时才会发生隐式缩小转换在上变量。在这些情况下,隐式转换很好,因为我们不想一直编写像 byte b =(byte)0 这样的代码。同时,如果我们编写类似 byte b = 128 的内容,我们会被警告,因为它没有直观的行为。

The implicit narrowing conversions only occur when you're assigning a constant expression to a variable. In these cases, implicit conversions are good as we don't want to be writing code like byte b = (byte)0 all the time. At the same time, we do want to be warned if we write something like byte b = 128, as that doesn't have intuitive behaviour.

当我们没有分配常量表达式时(例如。 int x = 0; byte b = x; ),我们总是希望在我们进行潜在有损转换时收到警告,因为它们很危险 - 因此在这种情况下明确的转换也是有道理的。

When we're not assigning constant expressions (so eg. int x = 0; byte b = x;), we always want to be warned when we're doing a potentially lossy conversion, as they're dangerous - so explicit conversions in this case also make sense.

这篇关于为什么允许从int到byte的隐式缩小转换,而不是从long到int?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆