This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Java in General and the fly likes Clarification On TypeCasting of ArrayList in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Clarification On TypeCasting of ArrayList in Java" Watch "Clarification On TypeCasting of ArrayList in Java" New topic
Author

Clarification On TypeCasting of ArrayList in Java

john kerl
Greenhorn

Joined: Jul 29, 2011
Posts: 13
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

Joined: Oct 13, 2005
Posts: 36453
    
  15
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

Joined: Oct 13, 2005
Posts: 36453
    
  15
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

Joined: Apr 06, 2010
Posts: 4240
    
    7

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

Joined: Jul 28, 2011
Posts: 9
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

Joined: Jul 29, 2011
Posts: 13
Thanks Guys..!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Clarification On TypeCasting of ArrayList in Java
 
Similar Threads
Saving and retrieving formatted data from database
Curious about an answer explanation..
mock test for generics & collections
Warnings during Generic assignments
Generics Doubt (Casting)