Java Tutorials - Herong's Tutorial Examples - Version 7.03, by Dr. Herong Yang
Raw Type, Generic Type and Parameterized Type
This section provides a tutorial example on how legacy applications can continue to use raw types instead of parameterized types. But compiler will give you 'unchecked or unsafe operations' warnings.
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.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 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.java 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 extends Object declared in 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 extends Object declared in class Vector RawTypeTest.java:10: warning: [unchecked] unchecked call to add(E) as a member of the raw 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] unchecked call to add(E) as a member of the raw type Vector myList.add(new Float(3.99)); ^ where E is a type-variable: E extends Object declared in class Vector 4 warnings
Last update: 2014.
Table of Contents
Execution Process, Entry Point, Input and Output
Primitive Data Types and Literals
Bits, Bytes, Bitwise and Shift Operations
Managing Bit Strings in Byte Arrays
Reference Data Types and Variables
StringBuffer - The String Buffer Class
System Properties and Runtime Object Methods
►Generic Classes and Parameterized Types
Using a Generic Class - Example
Creating a Generic Class - Example
►Raw Type, Generic Type and Parameterized Type
Parameterized Type and Subtyping
Wildcard Parameterized Type Test
Wildcard Parameterized Subtyping
Wildcard Parameterized Subtyping Example
Generic Methods and Type Inference
Lambda Expressions and Method References
Execution Threads and Multi-Threading Java Programs
ThreadGroup Class and "system" ThreadGroup Tree
Synchronization Technique and Synchronized Code Blocks
Deadlock Condition Example Programs