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

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

问题描述

我有以下代码片段。

  public static void main(String [] args){
short a = 4;
short b = 5;
short c = 5 + 4;
short d = a;
short e = a + b; //不编译(表达式处理为int)


short z = 32767;
short z_ = 32768; //不编译(超出范围)

test(a);
test(7); //不编译(不适用于arg int)
}

public static void test(short x){}

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




  • 只有使用文字或单个变量(只要值在声明的类型范围内)才可以直接初始化而不进行强制转换。

  • 如果分配的rhs处理表达式


  • 但是为什么我需要转换第二个方法调用的参数,同时考虑到前面的总结?

    解决方案

    这些是相关的JLS部分:



    JLS 5.1.1身份转换 h3>


    允许将任何类型的类型转换为相同类型。




    JLS 5.2分配转换




    分配给一个变量:表达式的类型必须转换为变量的类型。作业上下文允许使用以下之一:




    • 身份转换

    • < ]


    此外,如果表达式是 byte short char int




    • 如果变量的类型为 byte short char ,并且常量表达式的值可在变量类型中表示。


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

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

    short z = 32767; // representable constant
    short z_ = 32768; //不能编译!不可表示的常数

    至于为什么不编译:

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

    这是因为缩小的常量转换只定义了赋值;不适用于方法调用,其规则完全不同。



    JLS 5.3。方法调用转换




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


    解释方法解析如何正确运作,我将引用 Effective Java第2版,第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天全站免登陆