Java - 简短和铸造 [英] Java - short and casting

查看:24
本文介绍了Java - 简短和铸造的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码片段.

public static void main(String[] args) {短 a = 4;短 b = 5;短 c = 5 + 4;短 d = a;短 e = a + b;//不编译(表达式被视为 int)短 z = 32767;短 z_ = 32768;//不编译(超出范围)测试(一);测试(7);//不编译(不适用于 arg int)}公共静态无效测试(短 x){}

以下摘要是否正确(仅针对上面使用短的示例)?

  • 不强制转换的直接初始化只能使用文字或单个变量(只要值在声明类型的范围内)
  • 如果赋值的 rhs 处理使用变量的表达式,则需要强制转换

但是为什么我需要在考虑到之前的摘要的情况下强制转换第二个方法调用的参数?

解决方案

这些是相关的 JLS 部分:

JLS 5.1.1 身份转换

<块引用>

任何类型都允许从一个类型转换为同一个类型.

JLS 5.2 赋值转换

<块引用>

赋值转换发生在将表达式的值赋给变量时:表达式的类型必须转换为变量的类型.分配上下文允许使用以下之一:

  • 身份转换
  • [...]

另外,如果表达式是byteshortcharint类型的常量表达式:

  • 如果变量的类型是 byteshortchar,并且常量表达式可以用变量的类型来表示.

以上规则解释了以下所有内容:

short a = 4;//可表示的常量短 b = 5;//可表示的常量短 c = 5 + 4;//可表示的常量短 d = a;//身份转换短 e = a + b;//不编译!加法结果是 int短 z = 32767;//可表示的常量短 z_ = 32768;//不编译!不可表示的常数

至于为什么这不能编译:

test(7);//不编译!没有 test(int) 方法!

这是因为constant的收缩转换只是为赋值定义的;不是用于方法调用,它有完全不同的规则.

JLS 5.3.方法调用转换

<块引用>

方法调用转换特别不包括作为赋值转换一部分的整数常量的隐式缩小.Java 编程语言的设计者认为,包含这些隐式收缩转换会增加重载方法匹配解析过程的复杂性.

我不会解释方法解析的精确工作原理,而是引用Effective Java 2nd Edition,第 41 条:明智地使用重载:

<块引用>

决定选择哪个重载的规则极其复杂.它们在语言规范中占三十三页,很少有程序员了解它们的所有细微之处.


另见

I have the following code snippet.

public static void main(String[] args) {
 short a = 4;
 short b = 5;
 short c = 5 + 4;
 short d = a;
 short e = a + b; // does not compile (expression treated as int)


 short z = 32767;
 short z_ = 32768; // does not compile (out of range)

 test(a);
 test(7); // does not compile (not applicable for arg int)
}

public static void test(short x) { }

Is the following summary correct (with regard to only the example above using short)?

  • direct initializations without casting is only possible using literals or single variables (as long as the value is in the range of the declared type)
  • if the rhs of an assignment deals with expressions using variables, casting is necessary

But why exactly do I need to cast the argument of the second method call taking into account the previous summary?

解决方案

These are the relevant JLS sections:

JLS 5.1.1 Identity Conversion

A conversion from a type to that same type is permitted for any type.

JLS 5.2 Assignment Conversion

Assignment conversion occurs when the value of an expression is assigned to a variable: the type of the expression must be converted to the type of the variable. Assignment contexts allow the use of one of the following:

  • Identity conversion
  • [...]

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

  • 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.

The above rules explain all of the following:

short a = 4;     // representable constant
short b = 5;     // representable constant
short c = 5 + 4; // representable constant
short d = a;     // identity conversion
short e = a + b; // DOES NOT COMPILE! Result of addition is int

short z  = 32767; // representable constant
short z_ = 32768; // DOES NOT COMPILE! Unrepresentable constant

As to why this doesn't compile:

test(7); // DOES NOT COMPILE! There's no test(int) method!

It's because the narrowing conversion with constant is only defined for assignments; not for method invocation, which has entirely different rules.

JLS 5.3. Method Invocation Conversion

Method invocation conversions specifically do not include the implicit narrowing of integer constants which is part of assignment conversion. The designers of the Java programming language felt that including these implicit narrowing conversions would add additional complexity to the overloaded method matching resolution process.

Instead of explaining how method resolution works precisely, I will just quote Effective Java 2nd Edition, Item 41: Use overloading judiciously:

The rules that determine which overloading is selected are extremely complex. They take up thirty-three pages in the language specification, and few programmers understand all of their subtleties.


See also

这篇关于Java - 简短和铸造的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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