aspose file tools*
The moose likes Beginning Java and the fly likes Generics and class cast exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Generics and class cast exception" Watch "Generics and class cast exception" New topic
Author

Generics and class cast exception

Kalyan Anand
Ranch Hand

Joined: Feb 07, 2007
Posts: 194
By the use of generics can we expect that we don't have class cast exceptions ?
Thomas Thevis
Ranch Hand

Joined: Sep 02, 2008
Posts: 87
As long as you don't mix generics with non-generic code, the compiler should take care of class cast problems.


SCJP 5.0, SCJD in progress
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14104
    
  16

Generics let you avoid casting in many cases, and if you don't cast, you can't get a ClassCastException.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Harshit Rastogi
Ranch Hand

Joined: Apr 15, 2008
Posts: 131
The main aim of Generics introduction was to reduce the ClassCastException at runtime.
In Makority of cases it will identify the problem at compile time but the problem (error) can come if you use Collections which are Generic and which are not.

Eg



to overcome this listWithOutGen make is also typesafe.


<a href="http://technologiquepanorama.wordpress.com" target="_blank" rel="nofollow">My Techie Blog</a><br /><a href="http://www.java-questions.com" target="_blank" rel="nofollow">Java Questions</a>
Harshit Rastogi
Ranch Hand

Joined: Apr 15, 2008
Posts: 131

to overcome this listWithOutGen make is also typesafe.[/QB]


rectify above line

to overcome this make listWithOutGen is also typesafe
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Generics and class cast exception