Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Clarification On TypeCasting of ArrayList in Java

 
john kerl
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
I need a clarification regarding the below code.

1. ArrayList<Car> myCar = new ArrayList<Car>(); // Car is a java class which extends Vehicle class[/b]
2. ArrayList<Bus> myBus = new ArrayList<Bus>(); // Bus is a java class which extends Vehicle class
3. myCar.add(new Car("test"));
4. ArrayList d = myCar;
5. myBus = (ArrayList<Bus>)d;

In line 5., we observe that the ArrayList d is able to be typecasted to another form of ArrayList<Bus>. I am not clear on this as if we consider the below scenario, the typecasting is not possible. Can anybody please suggest me on this..!

Car myCar = new Car();
Bus myBus = new Bus();
Car car1 = (Car)myBus; // It will not compile as it cannot typecast this..!

Thanks,
JKoder70014


 
Campbell Ritchie
Sheriff
Pie
Posts: 48955
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are not casting that List to another type. You are casting it to the same type it always had. Generic types are invariant, not covariant, as you will find from the Java™ Tutorials.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48955
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I was mistaken, because you started with a raw type. Anyway, go through the Java™ Tutorials section, and it explains about subtyping List<Foo>s.
 
Matthew Brown
Bartender
Posts: 4567
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, it's because you use a raw (non-generic) type on line 4. That is allowed by the compiler so that generic code can be backwards compatible with pre-generic code. So an ArrayList<Car> can be assigned to an ArrayList, and then because the information is now lost this can be assigned to an ArrayList<Bus>.

Since the generic type isn't known at run-time (type-erasure), you won't get an error when you try and add a Bus to it either.

The bottom line - don't do this! Don't mix generic and non-generic code unless you have no choice, as you lose the type safety.
 
shiva prasad.
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
java koder wrote:Hi All,
I need a clarification regarding the below code.

1. ArrayList<Car> myCar = new ArrayList<Car>(); // Car is a java class which extends Vehicle class[/b]
2. ArrayList<Bus> myBus = new ArrayList<Bus>(); // Bus is a java class which extends Vehicle class
3. myCar.add(new Car("test"));
4. ArrayList d = myCar;
5. myBus = (ArrayList<Bus>)d;

In line 5., we observe that the ArrayList d is able to be typecasted to another form of ArrayList<Bus>. I am not clear on this as if we consider the below scenario, the typecasting is not possible. Can anybody please suggest me on this..!

Car myCar = new Car();
Bus myBus = new Bus();
Car car1 = (Car)myBus; // It will not compile as it cannot typecast this..!

Thanks,
JKoder70014




If you have used generics while creation of array list d as

ArrayList<Vehicle> d = new ArrayList<Vehicle>();

then both line 4 and 5 will not compile, because generics allow explicit variance only.

In your code the array list d is created without specifying its generic type,that is why the compiler do not know its type
and line numbers 4 ,5 are compiled, which will throw run time exception.
 
john kerl
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Guys..!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic