Re: What's wrong with this sentence?



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
.



Relevant Pages

  • Re: foreach considered dangerous?
    ... > taget type without warning. ... > Without generics this behaviour had the advantage of less typing for us ... > consider this feature of foreach as dangerous. ... > implicit cast would occur. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: A question related to type casting
    ... cast" warning for the cast to a raw List. ... compiler passes the code, ... I prefer no warnings when I compile, but they aren't a restriction on the code itself, and I'm not seeing the warning you describe anyway. ... The only reason it works now is that generics are strictly compile-time, and so there's not actually any difference between the raw List type and a parameterized reference to the generic List. ...
    (comp.lang.java.programmer)
  • generics and type checking
    ... I'm giving generics a shot, and I'm getting this warning: Type safety: ... The cast from Object to ArrayListis actually checking against ... gained by adding the 4 instances of <RequestBean> was not having to cast to ...
    (comp.lang.java.programmer)
  • Re: Error in the equal method of generic Pair class
    ... Because there's no way to actually check that cast. ... You could cast to Pair, suppress the warning, and hope that equals ... You could cast to Pair, without type parameters, and suppress the warning ... Or, you could enter the voodoo wonderland of expert generics, and cast to ...
    (comp.lang.java.programmer)
  • Re: How to implement generics with clone()?
    ... >I need to use cloneon generics but I don't know how to do. ... > it gives me the warning: ... I can understand this because clone() returns an Object. ... the cast is always unsafe and will always result in a warning. ...
    (comp.lang.java.programmer)