wood burning stoves 2.0*
The moose likes Java in General and the fly likes Type Information Not available at runtime. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Type Information Not available at runtime." Watch "Type Information Not available at runtime." New topic
Author

Type Information Not available at runtime.

amol deshpande
Ranch Hand

Joined: Sep 05, 2003
Posts: 162
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

Joined: Dec 27, 2007
Posts: 69
Check this example, it will give you Class cast exception



Java-JavaEE Hibernate Spring Spring Roo Web Applications
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
amol deshpande
Ranch Hand

Joined: Sep 05, 2003
Posts: 162
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

Joined: Oct 27, 2005
Posts: 19651
    
  18

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Type Information Not available at runtime.
 
Similar Threads
Generics doubt in K&B p.574
collection and generics
Type of the Generic List
Generics
Generics