Java Raw Types and Legacy Code

In this chapter you will learn:

  1. How to use generic class in a non-generic way
  2. Example - Java Raw Types

Deal with legacy code

To handle the transition to generics, Java allows a generic class to be used without any type arguments.

Example

Here is an example that shows a raw type in action:


class MyClass<T> {
  T ob;//from  w  ww  .  ja  v  a2s .c  om
  MyClass(T o) {
    ob = o;
  }
  T getob() {
    return ob;
  }
}
public class Main {
  public static void main(String args[]) {
    MyClass raw = new MyClass(new Double(98.6));
    double d = (Double) raw.getob();
    System.out.println("value: " + d);
  }
}

Output:

Next chapter...

What you will learn in the next chapter:

  1. How to extend generic class
  2. Example - Using a Generic Superclass
  3. How to be a generic subclass
  4. Run-Time Type Comparisons Within a Generic Hierarchy
  5. How to override methods in a generic class
Home »
  Java Tutorial »
    Java Langauge »
      Java Generics
Java Generic Type
Java Generic Bounded Types
Java Generic Method
Java Generic Constructors
Java Generic Parameter Wildcard
Java Generic interface
Java Raw Types and Legacy Code
Java Generic Class Hierarchies
Java generic types cast
Java Generic Restrictions