Java扩展转换 [英] Java widening conversions

查看:529
本文介绍了Java扩展转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在准备Java 7认证并提出以下问题。

I'm preparing for Java 7 certification and have the following question.

字节b = 10 编译好。看起来编译器正在缩小int 10到byte 10然后装箱。怎么来字节b =新的字节(10)不会编译?为什么编译器不能像第一种情况那样缩小int 10到byte 10?

Byte b = 10 compiles ok. Looks like the compiler is narrowing int 10 to byte 10 and then boxing it. How come Byte b = new Byte(10) won't compile? Why can't the compiler narrow int 10 to byte 10 like it did in the first case?

另外 Long l = new Long( 10)编译好但长l = 10 失败?

我不是明确这是如何工作的。有人可以提供明确的解释吗?

I'm not clear about how this works. Can somebody provide an clear explanation?

推荐答案

JLS的第5.2节涵盖了分配上下文中允许的转换类型。

Section 5.2 of the JLS covers the types of conversions that are allowed in assignment contexts.


分配上下文允许使用以下之一:

Assignment contexts allow the use of one of the following:


  • 身份转换(第5.1.1节)

  • an identity conversion (§5.1.1)

扩大原始转换(第5.1.2节)

a widening primitive conversion (§5.1.2)

扩大参考转换(§5.1.5)

a widening reference conversion (§5.1.5)

拳击转换(§5.1.7)可选地后跟加宽参考转换

a boxing conversion (§5.1.7) optionally followed by a widening reference conversion

此外,


此外,如果表达式是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,常量表达式的值可以在变量的类型中表示。

  • A narrowing primitive conversion may be used if the type of the variable is byte, short, or char, and the value of the constant expression is representable in the type of the variable.

缩小原语如果变量的类型是:

A narrowing primitive conversion followed by a boxing conversion may be used if the type of the variable is:


  • 字节和常量表达式的值,则可以使用转换后跟装箱转换可以在类型字节中表示。

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

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

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.

字节b = 10 编译好,因为 10 是一个常量表达式,可表示为字节

Byte b = 10 compiles ok because 10 is a constant expression and is representable as a byte.

字节b = new Byte(10)将无法编译,因为 10 int literal ,方法调用转换不会执行原始缩小转换。要调用 Byte 构造函数进行编译,可以显式地将 10 强制转换为字节

Byte b = new Byte(10) won't compile because 10 is an int literal, and method invocation conversion won't perform primitive narrowing conversions. To get this call to a Byte constructor to compile, you can explicitly cast 10 to byte:

Byte b = new Byte( (byte) 10);

Long l = new Long(10)编译因为方法调用转换将执行原始扩展转换,包括从 int long

Long l = new Long(10) compiles because method invocation conversion will perform primitive widening conversions, including from int to long.

Long l = 10 将无法编译,因为Java不会特别允许进行扩展转换,然后进行装箱转换,我在最近的回答中讨论过。要使其编译,您可以使用 long 文字,因此只需要装箱。

Long l = 10 won't compile, because Java will not specifically allow a widening conversion followed by a boxing conversion, as I discussed in a recent answer. To get this to compile, you can use a long literal, so only boxing is necessary.

Long l = 10L;

这篇关于Java扩展转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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