Reputation: 2062
What is the difference between this lines:
List<Object> list = new ArrayList<Object>();
and
List list = new ArrayList();
As i understand, all is object and when you do just List list, you just mentioned that.
Upvotes: 2
Views: 2674
Reputation: 71
Difference between List
and List<T>
is:
List
is a raw type and thus can contain an object of any type whereas List<T>
may contain an object of type T or subtype of T.List
reference variable, whereas we can only assign a list object of type <T>
to a reference variable of type List<T>
.Eg: List list=new ArrayList(); //works fine
List<String> list1=new ArrayList(); //works fine
List<Animal> animals=new ArrayList<Animal>(); //works fine
List<Animal> dogs=new ArrayList<Dog>(); // compile-time error
Upvotes: 0
Reputation: 1530
List
which is Rawtype is not exactly the same thing as List<Object>
. As already mentioned List
is a java legacy code and since backward compatibility is a major concern in Java, the usage of Rawtypes such as List
is still permitted, whereas List<Object>
is a List of Objects. If your using the List
class methods on both objects you'll notice that they behave the sameway, there is not difference. But if you try to do something like this in with JDK 8+ for instance:
List<Object> objects = new ArrayList<>();
List<String> strings = objects; //Compilation error
You'll get a compiler error, whereas the bellow code works fine:
List raws = new ArrayList();
List<String> string = raws; // No compilation errors
The reason the second example works is for people who maintain the legacy code that is using List
for instance. They can affect it to concret classes and work with them instead of keep working with the Rawtype Class.
From java docs :
To facilitate interfacing with non-generic legacy code, it is possible to use as a type the erasure (§4.6) of a parameterized type (§4.5) or the erasure of an array type (§10.1) whose element type is a parameterized type. Such a type is called a raw type.
Upvotes: 0
Reputation: 4737
There's only a fool's difference. Using List<Object>
and the non-generic List
is silly, because what are you going to do with it?
Normally, you use a List
to group related items and later, loop over them in some way and perform some common operations or extract some common information for every item.
This makes at least some sense:
List<Car> cars = getMeTheCars();
for(Car c : cars) {
System.out.println(c.getName() + " is a car.");
}
If there are a bunch of different, unrelated types in your list, you get something horrible like this:
List<Object> obs = getMeTheObjects();
for(Object o : obs) {
if(o instanceof String) { ... }
if(o instanceof DateTime) { ... }
if(o instanceof Integer) { ... }
if(o instanceof Double) { ... }
if(o instanceof Decimal) { ... }
if(o instanceof SomeWeirdCustomClass) { ... }
if(o instanceof SomeOtherClass) { ... }
}
Upvotes: 1
Reputation: 11867
While you won't see a difference in the how the List
/ArrayList
itself behaves, the use of raw types can impact other areas of your program, as all generic checks involving the code in question are skipped.
Take this question for example. From the accepted answer:
public class Test {
public static void main(String[] args) {
String s;
// this compiles
s = new Generic<Object>().get();
// so does this
s = new Generic<Object>().<String>get();
// this doesn't compile
s = new Generic().get();
// neither does this
s = new Generic().<String>get();
}
}
class Generic<A> {
<B> B get() { return null; }
}
Note how when the class Generic
is parameterized with <Object>
, you can compile the code just fine, but when you use the raw type the unrelated generic type B
declared inside Generic
is also erased, thus causing compile-time errors as Object
can't be assigned to String
.
So there is a difference -- when using raw types, you don't get any checks. If you use <Object>
, the compiler will still perform type checks in other parts of your code.
Upvotes: 1
Reputation: 11907
Java has been very good at keeping backwards compatibility between its releases. Some would say, too good ;)
When Generics was added Sun put a lot of effort into making previous versions of Java work in just the same way. They also made sure that Generics was part of the Java compiler, and not the JVM. So a few oddities show through, and this is one of them.
The result is that the two are practically identical. All in the name of backwards compatibility of Java language versions.
Upvotes: 3
Reputation: 80
Basically only the type differs. When you use List Object you are using the Object type (in this case). When you dont say the list will be default made to be RAW type. Hope this helps.
Upvotes: 0
Reputation: 41178
In practical terms there is no difference, they both compile down to the exact same code.
List<Object>
though is showing that you have thought about what will go in the list and know it could be anything, whereas List
on its own shows nothing.
Upvotes: 6
Reputation: 121998
Ultimately both statements are same. In this case there is no difference since every class in java extends Object.
You'll see the difference , if you take any specific object other than Object
.
For ex:
List<String> vs List
Upvotes: 6