I am new to java and wanted to know difference between
Stack<Integer>st=new Stack<Integer>();
and
Stack<Integer>st=new Stack<>();
I know polymorphism but don't exactly know how above statements are different.
I am new to java and wanted to know difference between
Stack<Integer>st=new Stack<Integer>();
and
Stack<Integer>st=new Stack<>();
I know polymorphism but don't exactly know how above statements are different.
From the Java Tutorials generics lesson:
In Java SE 7 and later, you can replace the type arguments required to invoke the constructor of a generic class with an empty set of type arguments (
<>
) as long as the compiler can determine, or infer, the type arguments from the context. This pair of angle brackets,<>
, is informally called the diamond. For example, you can create an instance of Box with the following statement:
Box<Integer> integerBox = new Box<>();
This is the same. Starting from Java 7 those types in declaration are redundant as they are inferred automatically : type-inference-generic-instance-creation
For example, consider the following variable declaration:
Map<String, List<String>> myMap = new HashMap<String, List<String>>();
In Java SE 7, you can substitute the parameterized type of the constructor with an empty set of type parameters (<>):
Map<String, List<String>> myMap = new HashMap<>();
But you still can use both for Java 7+