This discussion is archived
2 Replies Latest reply: Aug 4, 2010 4:39 AM by 843793 RSS

Generics, still not clear how to force the correct data type

843793 Newbie
Currently Being Moderated
Why i don't have class cast error in runtime? And if it's normal, how can I define a code that notice it?

Thanks a lot
Salvatore
class myGeneric<T> {

    private ArrayDeque<T> array;

    public myGeneric(int numElements) {
        array = new ArrayDeque<T>(numElements);
    }

    public void add(T newValue) {
        array.add(newValue);
    }

    public static void main(String[] args) {

        myGeneric<Integer> myDeque = new myGeneric<Integer>(100);
        DataFiller<Integer> myFiller = new DataFiller<Integer>(myDeque);



    }
}

class DataFiller<T> {

    DataFiller(myGeneric<T> deque) {

        //no error in runtime        
        deque.add((T) "Test");
    }
}
  • 1. Re: Generics, still not clear how to force the correct data type
    794029 Newbie
    Currently Being Moderated
    Ziomuschio wrote:
    Why i don't have class cast error in runtime? And if it's normal, how can I define a code that notice it?

    Thanks a lot
    Salvatore
    class myGeneric<T> {
    
    private ArrayDeque<T> array;
    
    public myGeneric(int numElements) {
    array = new ArrayDeque<T>(numElements);
    }
    
    public void add(T newValue) {
    array.add(newValue);
    }
    
    public static void main(String[] args) {
    
    myGeneric<Integer> myDeque = new myGeneric<Integer>(100);
    DataFiller<Integer> myFiller = new DataFiller<Integer>(myDeque);
    
    
    
    }
    }
    
    class DataFiller<T> {
    
    DataFiller(myGeneric<T> deque) {
    
    //no error in runtime        
    deque.add((T) "Test");
    }
    }
    you are casting the String "Test" to type ("T")
    deque.add((T) "Test");
    So of course it won't give an error.
    if you want to do some sort of test i you DataFiller, perhaps something like this:
    (note, i changed your class types to begin with a CAPITAL letter, as a naming convention)
    class MyGeneric<T> {
     
        private ArrayDeque<T> array;
     
        public MyGeneric(int numElements) {
            array = new ArrayDeque<T>(numElements);
        }
     
        public void add(T newValue) {
            array.add(newValue);
        }
     
        public static void main(String[] args) {
     
            MyGeneric<Integer> myDeque = new MyGeneric<Integer>(100);
            DataFiller<Integer> myFiller = new DataFiller<Integer>(myDeque, 25); // given number "25" to start
     
     
     
        }
    }
     
    class DataFiller<T> {
     
        DataFiller(MyGeneric<T> deque, T myArgument) {
              deque.add(myArgument);
        }
    }
    I don't see why you are using the DataFiller class though... seems kinda weird.
  • 2. Re: Generics, still not clear how to force the correct data type
    843793 Newbie
    Currently Being Moderated
    Hi,

    If you want to see a ClassCastException, you should use what you've inserted in the queue.
    I've slightly modified the code to peek the queue for its head, which is expected to be an Integer.
    class MyGeneric<T> {
     
        final ArrayDeque<T> array;
     
        public MyGeneric(int numElements) {
            this.array = new ArrayDeque<T>(numElements);
        }
     
        public void add(T newValue) {
            this.array.add(newValue);
        }
     
        public static void main(String[] args) {
     
            MyGeneric<Integer> myDeque = new MyGeneric<Integer>(100);
            DataFiller<Integer> myFiller = new DataFiller<Integer>(myDeque);
            int head = myDeque.array.peek().intValue(); //ClassCastException
        }
    }
     
    class DataFiller<T> {
     
        DataFiller(MyGeneric<T> deque) {
            //no error in runtime
            deque.add((T)"Test");
        }
    }
    When you compile this code with -Xlint the compiler raises an unchecked cast warning along with the problematic statement.
    Warnings about generics should not be ignored.