Re: What is "evil" about casting?
From: Michael Borgwardt (brazil_at_brazils-animeland.de)
Date: 09/29/04
- Next message: Jcollins387: "class assignment"
- Previous message: Esco: "allow me to reintroduce myself my name is...."
- In reply to: Jonck: "What is "evil" about casting?"
- Next in thread: Daniel Sjöblom: "Re: What is "evil" about casting?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Wed, 29 Sep 2004 16:31:59 +0200
Jonck wrote:
> Could someone give me a brief explenation as to what is wrong with
> casting and how generics in Java 1.5 will solve this? Personally I don't
> see what's wrong with casting, in all the books I've read on Java casts
> are used all the time...
The problem with casting is that it results in runtime exceptions.
These may occur only under very rare circumstances, so in the
worst case, a bug may stay undetected in all tests and then suddenly
manifest at the worst possible time in production and cost millions
of dollars.
Therefore, Java goes to great lengths to convert runtime exceptions
to compile-time exceptions wherever possible, partially via type checks.
An embarassing deviation from this philosophy was the Collections
framework, which required lots of casting and was impossible to use in
a typesafe way without writing lots of wrapper classes.
Generics makes Collections (and similar classes) typesafe *and* convenient
to use - you can declare a List that can contain only Strings, and
the compiler will protest if you try to put anything else into it.
- Next message: Jcollins387: "class assignment"
- Previous message: Esco: "allow me to reintroduce myself my name is...."
- In reply to: Jonck: "What is "evil" about casting?"
- Next in thread: Daniel Sjöblom: "Re: What is "evil" about casting?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|