Reprint Address: 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, the Vector is raw type.
Raw types are supported in Java to keep legacy applications. In most cases, a raw type was equivalent a parameterized type with "Object" as the type argument.
This 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'll get:
C:\herong>javac Rawtypetest.java
Note:RawTypeTest.java uses unchecked or unsafe operations.
Note:recompile with-xlint:unchecked for details.
C:\herong>java rawtypetest
milk:3.99
As you can, the compiler let's use raw types. But It gives you the "unchecked or unsafe operations" warning. If you are recompile it with the '-xlint ', you'll be more details:
C:\herong>javac-xlint Rawtypetest.java rawtypetest.java:9: Warning: [rawtypes] found raw type:vector Vector my
List = new Vector (); ^ Missing type arguments for generic class vector<e> where E is a type-variable:e extends Object declared I
N class vector Rawtypetest.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 extend s Object declared in class Vector rawtypetest.java:10:warning: [unchecked] unchecked call to add (E) as a member of the RA
W type Vector mylist.add (new String ("Milk")); ^ where E is a type-variable:e extends Object declared in class Vector rawtypetest.java:11:warning: [Unchecked] un
Checked call to add (E) as a is the raw type Vector mylist.add (new Float (3.99)); ^ where E is a type-variable:e extends Object declared inClass Vector 4 warnings