Re: What's wrong with this sentence?



The intention behind using generics is to avoid surprises for the JVM.
i.e., You should use generics to avoid runtime(class cast) exceptions.
So in this case you are ought to get this warning in eclipse. There is
nothing wrong in type casting as well, as you are sure that the
attribute you are getting is what you have set before. Well, you can
avoid this warning by implementing Map interface and having something
like this:-

class ItemInfoMap extends HashMap<String,ItemInfo> implements
Map<String,ItemInfo>{
public ItemInfoMap put(String key, ItemInfo value) {
return super.put(key, value);
}
public ItemInfoMap get(String key) {
return super.get(key, value);
}

}


Now Use ItemInfoMap instead of Map :-)

ItemInfoMap userCart = (ItemInfoMap)
session.getAttribute("userCart");

Hope this helps!





Thanks!


On Oct 31, 1:17 pm, "JTL.zheng" <jtl.zh...@xxxxxxxxx> wrote:
Hashtable<String, ItemInfo> userCart = (Hashtable<String, ItemInfo>)
session.getAttribute("userCart");

I am using Eclipse

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?
how can I fix it?

Thank you very much in advance.


.



Relevant Pages

  • Re: Convert short to float
    ... >> avoid redundant casting if code compiles without ... > for an error, disable the warning (using a pragma, not a ... If you use a cast and later change the type ... it's quite dificult to avoid ...
    (microsoft.public.vc.language)
  • Get/SetWindowLongPtr
    ... I have to cast it from something to avoid ... This is happening because on non-win64, SetWindowLongPtr is actually ... compiler warning me the LONG_PTR -> LONG is a possible loss of data while ... So while this will work great on win64, there's no way to avoid a warning on ...
    (microsoft.public.win2000.developer)