Click here to Skip to main content
15,915,763 members
Please Sign up or sign in to vote.
1.24/5 (3 votes)
See more:
ArrayList<class> Foo = new ArrayList<class>();
AND
ArrayList<class> Foo = new ArrayList<>();

What I have tried:

this form is not necessary!!!!!!!!!!!!!!!!!!!!!!!
Posted
Updated 2-Jan-18 4:20am
Comments
Richard MacCutchan 2-Jan-18 10:30am    
Both appear to be the same in my sample test. What results did you get?
Hellen Micheal 3-Jan-18 7:26am    
same

1 solution

Because you already specified the type once, you don't need to do it twice. The compiler knows the type. This is called "type inference": Type Inference for Generic Instance Creation[^]
Quote:
You can replace the type arguments required to invoke the constructor of a generic class with an empty set of type parameters (<>) as long as the compiler can infer the type arguments from the context. This pair of angle brackets is informally called the diamond.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900