• Post Reply Bookmark Topic Watch Topic
  • New Topic

Generics and class cast exception  RSS feed

 
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 ?
 
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.
 
Java Cowboy
Sheriff
Posts: 16078
88
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.
 
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
 
You've gotta fight it! Don't give in! Read this tiny ad:
The WEB SERVICES and JAX-RS Course
https://coderanch.com/t/690789/WEB-SERVICES-JAX-RS
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!