• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generics and class cast exception

 
Kalyan Anand
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the use of generics can we expect that we don't have class cast exceptions ?
 
Thomas Thevis
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As long as you don't mix generics with non-generic code, the compiler should take care of class cast problems.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15480
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Generics let you avoid casting in many cases, and if you don't cast, you can't get a ClassCastException.
 
Harshit Rastogi
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Harshit Rastogi
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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


rectify above line

to overcome this make listWithOutGen is also typesafe
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic