What is the difference between List <String> stringList = new ArrayList <String> () and List <String> stringList = new ArrayList ()?

I know that in java the following two declarations and initialization are possible.

List<String> stringList = new ArrayList<String>() 

List<String> stringList = new ArrayList()?

But what exactly are the differences between them?

Also, what do you prefer to name list variables? Like you, do you prefer to call them variables or just variables? Or do you have a different approach?

+4
source share
4 answers

Actually there is no difference, although the second will give you a compilation warning.

They are compiled into the same bytecode, because the compiler throws out information about which parameters you used.

, ArrayList , , new ArrayList() . , ArrayList, , . - , .

, , , , , , .

Java 7, List<String> stringList = new ArrayList<>();, Java.

+11

: . , , .

? : , :

List<Puppy> puppies = new ArrayList<>() 

, : Java 7

List<Puppy> puppies = new ArrayList<Puppy>() 
+4

", , Java. , . :

List objList = new ArrayList();
objList.add(new Object());

List<String> list = objList;

, , List? ! , , , . ArrayList() . , - - , , , - String... "

& lt; String & gt; list1 = new ArrayList(); & lt; String & gt; list2 = ArrayList & lt; String & gt;()

+1

. ,

List<Double> t = new ArrayList(); 
List<String> stringList = new ArrayList(t);

, . erasure - , , Generics - ,

// This is a compile error.
List<Double> t = new ArrayList<Double>(); 
List<String> stringList = new ArrayList<String>(t);

, Java 7+ -

// This is a compile error.
List<Double> t = new ArrayList<>(); 
List<String> stringList = new ArrayList<>(t);
0

Source: https://habr.com/ru/post/1535133/


All Articles