Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Type Information Not available at runtime.

 
amol deshpande
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm thinking about following case:

void doStuff(ArrayList<Dob> dogList)
{

Dog d = dogList.get(0); // Here....
}

In example as above, we do not need a cast because dogList is type safe and dogList.get(0) always knows it's a dog that you are getting.
But at runtime the type information for generics is not available. So at runtime Dog d = doesn't know that it's a dog coming out there...so its equivalent to saying there exists a cast at runtime to a Dog type?

So it does give type safety at compile time, which is good for development, at runtime its no cheaper than using a non-generic , old type stuff?

I know its fair to say that there exists the benefit that at runtime we'll never get our old gold ClassCastException in case of generics, but Cast does exists?


 
Lalit Bhatt
Ranch Hand
Posts: 69
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check this example, it will give you Class cast exception

 
Rob Spoor
Sheriff
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
amol deshpande wrote:I'm thinking about following case:

void doStuff(ArrayList<Dob> dogList)
{

Dog d = dogList.get(0); // Here....
}

In example as above, we do not need a cast because dogList is type safe and dogList.get(0) always knows it's a dog that you are getting.
But at runtime the type information for generics is not available. So at runtime Dog d = doesn't know that it's a dog coming out there...so its equivalent to saying there exists a cast at runtime to a Dog type?

So it does give type safety at compile time, which is good for development, at runtime its no cheaper than using a non-generic , old type stuff?

I know its fair to say that there exists the benefit that at runtime we'll never get our old gold ClassCastException in case of generics, but Cast does exists?

The compiler adds the cast for you. If you decompile the class (e.g. using JAD) you see the cast.
 
amol deshpande
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,


List stringlIst = new ArrayList<String>();
stringlIst.add("Hello");
stringLIst(stringlIst);

public static void stringLIst(List<StringBuffer> strList)
{
for(StringBuffer sb :strList)
System.out.println(sb);
}


Even above stuff will throw ClassCast.

Thanks Rob for hint.....I was trying to figure out if at runtime do we any benefit pertaining to the case.

 
Rob Spoor
Sheriff
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
amol deshpande wrote:Even above stuff will throw ClassCast.

That's why the compiler gives you a warning about raw types. It's there for a reason, and your example shows you exactly that reason.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic