Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4468840

generics problem with casting

    XMLWordPrintable

Details

    • beta2
    • generic
    • solaris_7
    • Verified

    Description

      frog$ cat -n Test.java
           1 import java.util.*;
           2
           3 public class Test {
           4 public static void main(String[] args) {
           5 List<Test> l1 = new ArrayList<Test>();
           6 List<Test> l2 = (List<Test>)l1;
           7 }
           8 }
      frog$ newjavac -gj Test.java
      Test.java:6: interfaces java.util.List<Test> and java.util.List<Test> are incompatible; both define toArray(T[]), but with different return type
              List<Test> l2 = (List<Test>)l1;
                                          ^
      1 error
      frog$

      =============

      It doesn't seem to like to do == and != comparisons in some cases even
      though both objects are of the same type. Here is an example:

      import java.util.*;
      public class Test{
           public static void main(String[] args){
               Map<Integer,Set<Integer>> map=new
      HashMap<Integer,Set<Integer>>();
               map.put(new Integer(1),new HashSet<Integer>());
               map.put(new Integer(2),new HashSet<Integer>());
               if(map.get(new Integer(1))==map.get(new Integer(2))){
                   System.err.println("equal");
               }else{
                   System.err.println("not equal");
               }
           }
      }


      Here is the error produced:

      Test.java:7: interfaces java.util.Set<java.lang.Integer> and
      java.util.Set<java.lang.Integer> are incompatible; both define
      toArray(T[]), but with different return type
               if(map.get(new Integer(1))==map.get(new Integer(2))){
                                         ^
      1 error

      Attachments

        Activity

          People

            gafter Neal Gafter
            gafter Neal Gafter
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: