Smrita
Smrita

Reputation: 1279

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

I know that both of the following declaration and initialization are possible in java .

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

List<String> stringList = new ArrayList()?

But what exactly are the differences between them?

Also how do you prefer to name the list variables? As in do you prefer to name them variableList or just variables? Or do you have any other approach?

Upvotes: 6

Views: 2962

Answers (4)

Fred Hu
Fred Hu

Reputation: 161

"Yes, it is allowed to do it this way in order to achieve backward compatibility with previous Java versions. And no, you don't want to code like this. Consider the following:

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

List<String> list = objList;

The compiler would be happy to compile this, but guess what you get in List? An object! By declaring a Collection, you want to be sure that you get only Strings in there, while a raw type collections allow you to add anything. One day after refactoring your new ArrayList() will be returned from a separate method. Maybe the next day someone will add something to it and who knows, maybe this something won't be a String... "

Difference between List<String> list1 = new ArrayList(); and List<String> list2 = new ArrayList<String>()

Upvotes: 1

Elliott Frisch
Elliott Frisch

Reputation: 201447

They mix Generic and non-Generic types. Consider this,

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

Obviously, that may blow up at run-time. Type erasure is really a performance boost, but it means that Generics are a compile time check only,

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

Finally, in Java 7+ you can use the diamond operator -

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

Upvotes: 0

Dawood ibn Kareem
Dawood ibn Kareem

Reputation: 79838

There's really no difference, although the second one will give you a compile warning.

They compile to the same bytecode, because the compiler throws away the information about what type parameters you've used.

The problem is that the ArrayList class was invented before generics were, so we used to always have to create it as new ArrayList(), without specifying the type parameter. Now with generics, we can specify what type of objects will be stored in an ArrayList and have the compiler check that we use it correctly. The notation for doing that is what you've used in the first example.

But any checking that the compiler does subsequently is based on the type of the variable, not the class of what you've created, so the difference will have no effect at all, past the initial compiler warning.

From Java 7 onwards, the preferred way of writing this is List<String> stringList = new ArrayList<>(); but this notation is unavailable in earlier versions of Java.

Upvotes: 11

Jeroen Vannevel
Jeroen Vannevel

Reputation: 44439

Both those names aren't good: the name of your variable should reflect its contents. I already know it's a variable and I already know it's a list.

What is in the list? For example: if it's the bag of puppies I just bought at the pet store:

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

Notice how I omitted the type: that is possible since Java 7 and is exactly the same as

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

Upvotes: 4

Related Questions