Re: What's wrong with this sentence?
- From: Lew <lew@xxxxxxxxxxxxx>
- Date: Wed, 31 Oct 2007 09:06:43 -0400
cool_guy wrote:
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.
"JTL.zheng" wrote:
Hashtable<String, ItemInfo> userCart = (Hashtable<String, ItemInfo>)
session.getAttribute("userCart");
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 use HashMap.
I am using Eclipse
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
Hashtable<String, ItemInfo> userCart = (Hashtable)
session.getAttribute("userCart");
still get a warning too:
Type safety: The expression of type Hashtable needs unchecked
conversion to conform to
Hashtable<String,ItemInfo>
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.
how can I fix it?
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" );
--
Lew
.
- References:
- What's wrong with this sentence?
- From: JTL.zheng
- Re: What's wrong with this sentence?
- From: cool_guy
- What's wrong with this sentence?
- Prev by Date: Re: How to strip comments out of code
- Next by Date: Re: memory management
- Previous by thread: Re: What's wrong with this sentence?
- Next by thread: Re: What's wrong with this sentence?
- Index(es):
Relevant Pages
|