相关疑难解决方法(0)

为什么这个Java 8程序无法编译?

这个程序在Java 7(或Java 8中-source 7)编译得很好,但是无法用Java 8编译:

interface Iface<T> {}
class Impl implements Iface<Impl> {}

class Acceptor<T extends Iface<T>> {
    public Acceptor(T obj) {}
}

public class Main {
    public static void main(String[] args) {
        Acceptor<?> acceptor = new Acceptor<>(new Impl());
    }
}
Run Code Online (Sandbox Code Playgroud)

结果:

Main.java:10: error: incompatible types: cannot infer type arguments for Acceptor<>
        Acceptor<?> acceptor = new Acceptor<>(new Impl());
                                           ^
    reason: inference variable T has incompatible bounds
      equality constraints: Impl
      upper bounds: Iface<CAP#1>,Iface<T>
  where T is a type-variable:
    T …
Run Code Online (Sandbox Code Playgroud)

java generics type-inference java-8

77
推荐指数
3
解决办法
2万
查看次数

推理变量具有不兼容的边界.Java 8编译器回归?

以下程序在Java 7和Eclipse Mars RC2 for Java 8中编译:

import java.util.List;

public class Test {

    static final void a(Class<? extends List<?>> type) {
        b(newList(type));
    }

    static final <T> List<T> b(List<T> list) {
        return list;
    }

    static final <L extends List<?>> L newList(Class<L> type) {
        try {
            return type.newInstance();
        }
        catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

使用javac 1.8.0_45编译器,报告以下编译错误:

Test.java:6: error: method b in class Test cannot be applied to given types;
        b(newList(type));
        ^
  required: List<T>
  found: CAP#1
  reason: inference …
Run Code Online (Sandbox Code Playgroud)

java type-inference compiler-errors java-8

21
推荐指数
3
解决办法
2万
查看次数

标签 统计

java ×2

java-8 ×2

type-inference ×2

compiler-errors ×1

generics ×1