7 Replies Latest reply: Aug 2, 2010 9:38 AM by 796085 RSS

    Generics not type safe?

    843793
      This is my class (simplified). When I create an istance and put a wrong type it works, is it normal???
      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);
         
         
         //wrong data type BUT no error???? why???
         myDeque.add(new String("test"));
        
       }
      
      }
        • 1. Re: Generics not type safe?
          EJP
          Is that your real code? Check.
          • 2. Re: Generics not type safe?
            843793
            No it is a simplified version that actually produce the expected error, this is another simplified version that doesn't recongnize the type error.

            Sorry for the mistake
            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 myFiller = new DataFiller(myDeque);
                    
            
            
                }
            }
            
            class DataFiller {
            
                DataFiller(myGeneric deque) {
                    //Wrong type, why no error???
                    deque.add("test");
                }
            }
            • 3. Re: Generics not type safe?
              EJP
              Exactly. The code you posted first doesn't compile, contrary to your assertion above. The code you just posted doesn't give an error because you have used myGeneric as a raw type, which gave you a warning about typesafety, which you ignored.
              • 4. Re: Generics not type safe?
                843793
                Thanks you very much for the reply.

                But I don't receive any warning with my IDE ( I have Netbeans 6.9) by the way it's not important.

                I just bother you some more because i don'tr understand what you mean when you say
                ejp wrote:
                you have used myGeneric as a raw type
                • 5. Re: Generics not type safe?
                  791266
                  Ziomuschio wrote:
                  Thanks you very much for the reply.

                  But I don't receive any warning with my IDE ( I have Netbeans 6.9) by the way it's not important.

                  I just bother you some more because i don'tr understand what you mean when you say
                  ejp wrote:
                  you have used myGeneric as a raw type
                  Raw type means that you have used the generified class myGeneric without specifying a type. In e.g.
                  DataFiller(myGeneric deque) {
                  The line above is where you is using it as a raw type. Your IDE should warn you about that, unless you have turned that off.

                  That line should say something like:
                  DataFiller(myGeneric<Integer> deque)
                  • 6. Re: Generics not type safe?
                    843793
                    Right. Thanks you very much. I didn't realize it.
                    I touhgt that the compiler has to force me to put the correct type. But in this way raw type generics wouldn't exist at all.
                    Salvatore
                    • 7. Re: Generics not type safe?
                      796085
                      As people have already said, the command line compiler will issue a warning unless you've used @SuppressWarnings to hide it. I don't know about other IDEs, but Eclipse will also issue a warning by default which you can either suppress or turn off completely.