Java Generic type/parameterized Type/raw Type__java

Source: Internet
Author: User

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 
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.