java Generic Type/Parameterized Type/Raw Type__java

來源:互聯網
上載者:User

轉載地址:http://www.herongyang.com/Java/Generic-Class-Raw-Generic-Parameterized-Type.html

What Is a Generic Type? A generic type is a generic class or interface that uses type parameters.

What Is a Parameterized Type? A parameterized type is a parameterized version of a generic class or interface. For example, Vector<String> is a parameterized type.

What Is a Raw Type? A raw type is a parameterized type with the parameter type argument omitted. For example, Vector is raw type.

Raw types are supported in Java to keep legacy applications working. In most cases, a raw type is equivalent a parameterized type with "Object" as the type argument.

Here is a legacy example that still uses a raw type of the Vector<E> generic class.

/* RawTypeTest.java - Copyright (c) 2014, HerongYang.com, All Rights Reserved. */import java.util.*;class RawTypeTest {   public static void main(String[] a) {      java.io.PrintStream o = System.out;      Vector myList = new Vector();      myList.add(new String("Milk"));      myList.add(new Float(3.99));      String name = (String) myList.get(0);      Float price = (Float) myList.get(1);      o.println(name+": "+price);   }}

Compile and run this example. You will get:

C:\herong>javac RawTypeTest.javaNote: RawTypeTest.java uses unchecked or unsafe operations.Note: Recompile with -Xlint:unchecked for details.C:\herong>java RawTypeTestMilk: 3.99

As you can see, the compiler let you to use raw types. But it gives you the "unchecked or unsafe operations" warning. If you recompile it with the "-Xlint", you will see more details:

C:\herong>javac -Xlint RawTypeTest.javaRawTypeTest.java:9: warning: [rawtypes] found raw type: Vector      Vector myList = new Vector();      ^  missing type arguments for generic class Vector<E>  where E is a type-variable:    E extends Object declared in class VectorRawTypeTest.java:9: warning: [rawtypes] found raw type: Vector      Vector myList = new Vector();                          ^  missing type arguments for generic class Vector<E>  where E is a type-variable:    E extends Object declared in class VectorRawTypeTest.java:10: warning: [unchecked] unchecked call to add(E) asa member of the raw type Vector      myList.add(new String("Milk"));                ^  where E is a type-variable:    E extends Object declared in class VectorRawTypeTest.java:11: warning: [unchecked] unchecked call to add(E) asa member of the raw type Vector      myList.add(new Float(3.99));                ^  where E is a type-variable:    E extends Object declared in class Vector4 warnings

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.