Re: What's wrong with this sentence?
Please do not top-post. Use trim-and-inline posting.
The intention behind using generics is to avoid surprises for the JVM.
It's to avoid surprises for the programmer, I should say.
i.e., You should use generics to avoid runtime(class cast) exceptions.
Well, you can
avoid this warning by implementing Map interface and having something
Not really a good idea.
Hashtable<String, ItemInfo> userCart = (Hashtable<String, ItemInfo>)
Do you need the synchronization that Hashtable provides? Even if you do,
making a synchronizedMap off HashMap is likely a better choice, if not just
This is a Java issue, not an Eclipse issue.
It get a warning:
Type safety: The cast from Object to Hashtable<String,ItemInfo> is
actually checking against the erased type
Hashtable<String, ItemInfo> userCart = (Hashtable)
still get a warning too:
Type safety: The expression of type Hashtable needs unchecked
conversion to conform to
what's wrong with this sentence?
Welcome to type erasure. Generic casts just don't work unless you suppress
the warning. At run time there is no generic information, so the cast is
"raw" anyway. An attempt to cast a raw type but assign to a generic type
makes the compiler cough. It's what people don't like about Java generics,
and there are proposals afoot to use "reified generics", i.e., generics that
work at run time.
Instead of implementing the cart as a Map, implement it as a custom type that
contains a Map. (Make sure it implements java.io.Serializable.) Change the
session object retrieval to
ShoppingCart userCart = (ShoppingCart) session.getAttribute( "userCart" );