Documentation

The Java™ Tutorials
Hide TOC
Raw Types原始类型
Trail: Learning the Java Language
Lesson: Generics (Updated)
Section: Generic Types

Raw Types原始类型

A raw type is the name of a generic class or interface without any type arguments. 原始类型是没有任何类型参数的泛型类或接口的名称。For example, given the generic Box class:例如,给定通用Box类:

public class Box<T> {
    public void set(T t) { /* ... */ }
    // ...
}

To create a parameterized type of Box<T>, you supply an actual type argument for the formal type parameter T:创建参数化类型的Box<T>,为形式类型参数T提供实际类型参数:

Box<Integer> intBox = new Box<>();

If the actual type argument is omitted, you create a raw type of Box<T>:如果省略了实际类型参数,则创建一个原始类型的Box<T>

Box rawBox = new Box();

Therefore, Box is the raw type of the generic type Box<T>. 因此,Box是泛型类型Box<T>的原始类型。However, a non-generic class or interface type is not a raw type.但是,非泛型类或接口类型不是原始类型。

Raw types show up in legacy code because lots of API classes (such as the Collections classes) were not generic prior to JDK 5.0. 原始类型出现在遗留代码中,因为许多API类(如Collections类)在JDK5.0之前不是泛型的。When using raw types, you essentially get pre-generics behavior — a Box gives you Objects. 当使用原始类型时,基本上会得到预泛型行为—Box为您提供ObjectFor backward compatibility, assigning a parameterized type to its raw type is allowed:为了向后兼容,允许将参数化类型指定给其原始类型:

Box<String> stringBox = new Box<>();
Box rawBox = stringBox;               // OK

But if you assign a raw type to a parameterized type, you get a warning:但如果将原始类型指定给参数化类型,则会收到警告:

Box rawBox = new Box();           // rawBox is a raw type of Box<T>
Box<Integer> intBox = rawBox;     // warning: unchecked conversion

You also get a warning if you use a raw type to invoke generic methods defined in the corresponding generic type:如果使用原始类型调用相应泛型类型中定义的泛型方法,也会收到警告:

Box<String> stringBox = new Box<>();
Box rawBox = stringBox;
rawBox.set(8);  // warning: unchecked invocation to set(T)

The warning shows that raw types bypass generic type checks, deferring the catch of unsafe code to runtime. 警告显示原始类型绕过泛型类型检查,将不安全代码的捕获延迟到运行时。Therefore, you should avoid using raw types.因此,应该避免使用原始类型。

The Type Erasure section has more information on how the Java compiler uses raw types.类型擦除部分提供了有关Java编译器如何使用原始类型的更多信息。

Unchecked Error Messages未选中的错误消息

As mentioned previously, when mixing legacy code with generic code, you may encounter warning messages similar to the following:如前所述,在将旧代码与通用代码混合时,您可能会遇到类似于以下内容的警告消息:

Note: Example.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

This can happen when using an older API that operates on raw types, as shown in the following example:当使用在原始类型上运行的旧API时,可能会发生这种情况,如以下示例所示:

public class WarningDemo {
    public static void main(String[] args){
        Box<Integer> bi;
        bi = createBox();
    }

    static Box createBox(){
        return new Box();
    }
}

The term "unchecked" means that the compiler does not have enough type information to perform all type checks necessary to ensure type safety. 术语“unchecked”表示编译器没有足够的类型信息来执行确保类型安全所需的所有类型检查。The "unchecked" warning is disabled, by default, though the compiler gives a hint. 默认情况下,“unchecked”警告被禁用,尽管编译器会给出提示。To see all "unchecked" warnings, recompile with -Xlint:unchecked.要查看所有“未选中”警告,请使用-Xlint:unchecked重新编译。

Recompiling the previous example with -Xlint:unchecked reveals the following additional information:使用-Xlint:unchecked重新编译前面的示例将显示以下附加信息:

WarningDemo.java:4: warning: [unchecked] unchecked conversion
found   : Box
required: Box<java.lang.Integer> bi = createBox();
                      ^
1 warning

To completely disable unchecked warnings, use the -Xlint:-unchecked flag. 要完全禁用未检查的警告,请使用-Xlint:-unchecked标志。The @SuppressWarnings("unchecked") annotation suppresses unchecked warnings. @SuppressWarnings("unchecked")注释禁止未检查的警告。If you are unfamiliar with the @SuppressWarnings syntax, see Annotations.如果您不熟悉@SuppressWarnings语法,请参阅注释


Previous page: Generic Types
Next page: Generic Methods