相关疑难解决方法(0)

什么是原始类型,为什么我们不应该使用它?

问题:

  • 什么是Java中的原始类型,为什么我经常听说不应该在新代码中使用它们?
  • 如果我们不能使用原始类型,它有什么替代方案,它是如何更好的?

java generics raw-types

617
推荐指数
13
解决办法
20万
查看次数

为什么这在Java7中编译而在Java8中不编译?

泛型是棘手的.看起来它们在不同版本的Java中被区别对待.

此代码在Java 7中成功编译,无法使用Java 8进行编译.

import java.util.EnumSet;

public class Main {
  public static void main(String[] args) {
    Enum foo = null;
    tryCompile(EnumSet.of(foo));
  }

  static <C extends Enum<C> & Another> void tryCompile(Iterable<C> i) {}

  static interface Another {}
}
Run Code Online (Sandbox Code Playgroud)

这是来自Java 8的错误消息.我用这个来编译它:http://www.compilejava.net/

/tmp/java_A7GNRg/Main.java:6: error: method tryCompile in class Main cannot be applied to given types;
    tryCompile(EnumSet.of(foo));
    ^
  required: Iterable<C>
  found: EnumSet
  reason: inferred type does not conform to upper bound(s)
    inferred: Enum
    upper bound(s): Enum<Enum>,Another
  where C is a type-variable: …
Run Code Online (Sandbox Code Playgroud)

java generics compiler-errors java-7 java-8

8
推荐指数
1
解决办法
2119
查看次数

标签 统计

generics ×2

java ×2

compiler-errors ×1

java-7 ×1

java-8 ×1

raw-types ×1