ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java – Boxing to Object类型以避免编译失败

2019-09-01 03:11:20  阅读:200  来源: 互联网

标签:java java-7 autoboxing


参见英文答案 > Why doesn’t autoboxing overrule varargs when using method overloading in Java 7?                                    3个
请考虑以下代码

public class Foo
{

  int value;
  public Foo (final String str, Object ... bug)
  {
    System.out.println ("Should work! 1");
  }

  public Foo (final String str, final int value, Object ... bug)
  {
     this.value = value;
    System.out.println ("Should work! 2");
  }

  public static void main (String[]args)
  {
    Foo f = new Foo ("Foo", 3); //Line 14
    System.out.println(f.value);
  }
}

当我使用jdk-1.6.x时,我成功地编译了它.
但升级到jdk-1.7后,它给了我错误:

Foo.java:18:错误:对Foo的引用是不明确的,Foo中的构造函数Foo(String,Object …)和Foo中的构造函数Foo(String,int,Object …)匹配
    Foo f = new Foo(“Foo”,3); //第14行

所以为了避免这个错误,我改变了第二个Ctor

  public Foo (final String str, final Integer value, Object ... bug)
  {
     this.value = value;
    System.out.println ("Should work! 2");
  }

这样它就可以自动装箱到Integer并跳过编译错误.

几个问题:
1)这是一个好习惯吗?如果没有那么还有其他方法吗?
2)为什么java开发人员会决定提供错误而不是允许它?

解决方法:

这是设计和Java 6和Java 7之间不兼容的已知区域,因为过载分辨率算法在Java 7中已得到修复.在Java 5和6中,以下代码无法编译:

class Test {
    void foo(int... i) {}
    void foo(double... d) {}

    void test() {
       foo(1,2,3);
    }
}

编译器会抱怨调用不明确.但这不是真的,因为double比int更通用,因此在这种情况下int是最具体的选项.

但是,此修复导致不兼容(请查看release notes;搜索“varargs”).结果是当你有int和Object时你不能选择最具体的方法,因为它们都不是另一个的子类型.所以这不应该编译.但是,您可以使用Integer,因为Integer是Object的子类型,在这种情况下,它是最具体的选项,因此编译器可以选择它.

发行说明中的​​相关部分:

While the javac compiler accepts more code than it did prior to JDK 7, this fix also results in a slight source incompatibility in the following case:

class Test {
    void foo(int... i) {}
    void foo(Object... o) {}

    void test() {
       foo(1,2,3);
    }
}

This code compiles in JDK 6 (the most specific method is foo(int...)). This code does not compile under JDK 7. As per 07001, it is not possible to choose between foo(int...) and foo(Object...) as neither int is a subtype of Object, nor Object is a subtype of int. This program should be disallowed (in fact, it should never have been allowed in the first place).

Nature of Incompatibility: behavioral and source

RFE: 07002

标签:java,java-7,autoboxing
来源: https://codeday.me/bug/20190901/1780418.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有