2 Replies Latest reply: Jul 23, 2010 3:21 AM by 843793 RSS

    Raw-type Vector assigned to parameterized variable

    843793
      It was my understanding that a parameterized-type variable may be assigned to a raw-type variable (because there is no restriction), but that a raw-type variable (which may point to various types in a collection) could not be assigned to a parameterized-type variable (as it should restrict to a certain type). However, consider the code below. The compiler allows a raw-type Vector with a String element to be assigned to a Vector of type Integer:

      //=======================================================
      import java.util.Vector;

      public class VectorGenericsTester
      {
           public static void main(String[] args)
           {
                // Integer-type Vector
                Vector<Integer> integerVector= new Vector<Integer>();

                // raw-type Vector (with String element)
                Vector rawVector = new Vector();
                rawVector.add("dummy string");

                // assign raw-type (with String element) to Integer-type Vector
                integerVector = rawVector;

                // String is inside of Integer-type Vector
                System.out.println(integerVector);

           }
      }

      //==========================================================

      This code throws an unchecked warning, but still allows the assignment. It seems that the compiler should throw an error instead. I suppose this conversion might be allowed in order to work with legacy code without the use of generics, but it seems like a great vulnerability for developers.

      Any comments are greatly appreciated!

      -Ryan
        • 1. This Thread is now moved
          EJP
          Note: This thread was originally posted in the [Java Compiler|http://forums.sun.com/forum.jspa?forumID=7] forum, but moved to this forum for closer topic alignment.
          • 2. Re: Raw-type Vector assigned to parameterized variable
            843793
            rbbrooks wrote:
            This code throws an unchecked warning, but still allows the assignment.
            And that's exactly the point. Generics guarantee that you won't get ClassCastExceptions from using them if and only if you don't use raw types anywhere and have no compiler warnings.

            Raw types have no point in new code (which is why you get a compiler warning) and are an easy way to break the guarantees of generics.
            It seems that the compiler should throw an error instead.
            By the way it doesn't "throw a warning" or "throw an error". The compiler produces a warning. "throw" is only used when talking about Exceptions (actually Throwable) that happen at runtime.
            I suppose this conversion might be allowed in order to work with legacy code without the use of generics, but it seems like a great vulnerability for developers.
            It's only a problem if you ignore the warning.