-2

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.

soorapadman
  • 4,451
  • 7
  • 35
  • 47
  • 1
    They're equivalent – Jaybird Dec 13 '18 at 10:00
  • Perhaps you should look at it :https://www.javaworld.com/article/2074080/core-java/core-java-jdk-7-the-diamond-operator.html – soorapadman Dec 13 '18 at 10:02
  • traditional syntax and diamond syntax – Ashvin solanki Dec 13 '18 at 10:03
  • 1
    And why did you ask the same question twice? And, for the future: you are expected to do serious research prior posting questions. Rest assured: when you are a newbie, anything you can dream of asking has been asked and answered here. And most likely, you can find zillions of books or tutorials explaining these things, too. – GhostCat Dec 13 '18 at 10:21

2 Answers2

0

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<>();

0

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+

Ashvin solanki
  • 4,802
  • 3
  • 25
  • 65
Akceptor
  • 1,914
  • 3
  • 26
  • 31