What is the meaning of the <?> token in Java?

前端 未结 8 1810
囚心锁ツ
囚心锁ツ 2020-12-18 01:47

What is the meaning of the token in this code copied from www.JavaPractices.com? When I replace it with the more conventional looking

8条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-18 02:17

    That is Generic Type... usually we set String, Object, or any other objects as generic types... but here they make it general. and generic type stands for the value it can store or hold. its generally used in Collections..

    well there is no much diff between the both.. - takes all kind of objects

                  -is also called `formal type parameter` makes use of what ever type of object you pass in .. for instance you can do this with  and not with 
    
    public class Box {
    
        private T t; // T stands for "Type"
    
        public void add(T t) {
            this.t = t;
        }
    
        public T get() {
            return t;
        }
    }
    

    for more see http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf and http://download.oracle.com/javase/tutorial/java/generics/gentypes.html

提交回复
热议问题