Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2007

Tip: Looking for answers? Try searching our database.

What's wrong with this sentence?

Thread view: 
JTL.zheng - 31 Oct 2007 09:17 GMT
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.
cool_guy - 31 Oct 2007 10:07 GMT
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!

> Hashtable<String, ItemInfo> userCart = (Hashtable<String, ItemInfo>)
> session.getAttribute("userCart");
[quoted text clipped - 18 lines]
>
> Thank you very much in advance.
Lew - 31 Oct 2007 14:06 GMT
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
[quoted text clipped - 10 lines]
>>
>> 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" );

Signature

Lew

Roedy Green - 31 Oct 2007 18:49 GMT
>Hashtable<String, ItemInfo> userCart = (Hashtable)
>session.getAttribute("userCart");

What happens when you cast it to a generic like this:

>Hashtable<String, ItemInfo> userCart = (Hashtable)
>session.getAttribute("userCart");
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Roedy Green - 31 Oct 2007 18:51 GMT
On Wed, 31 Oct 2007 17:49:51 GMT, Roedy Green
<see_website@mindprod.com.invalid> wrote, quoted or indirectly quoted
someone who said :

>>Hashtable<String, ItemInfo> userCart = (Hashtable)
>>session.getAttribute("userCart");
[quoted text clipped - 3 lines]
>>Hashtable<String, ItemInfo> userCart = (Hashtable)
>>session.getAttribute("userCart");

oops. In DSK Ctrl-W to send is right next to Ctrl-V to paste. It sent
this off before it was ready.
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Roedy Green - 31 Oct 2007 18:52 GMT
On Wed, 31 Oct 2007 17:49:51 GMT, Roedy Green
<see_website@mindprod.com.invalid> wrote, quoted or indirectly quoted
someone who said :

>>Hashtable<String, ItemInfo> userCart = (Hashtable)
>>session.getAttribute("userCart");

What happens when you cast it to a generic like this:

HashMap<String, ItemInfo> userCart = (HashMap<String, ItemInfo>)
session.getAttribute("userCart");

Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Zig - 31 Oct 2007 21:36 GMT
> Hashtable<String, ItemInfo> userCart = (Hashtable<String, ItemInfo>)
> session.getAttribute("userCart");
[quoted text clipped - 5 lines]
> actually checking against the erased type
>  Hashtable

Ultimately, generics are erased, and this compiles down to

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

You get the warning, since the runtime will only cast your  
"session.getAttribute" to Hashtable, and can not verify the contents of  
the table. Thus, a subsequent call to

ItemInfo info=userCart.getValue("somevalue");

*could* spuriously throw a ClassCastException (since the cast to ItemInfo  
is automatically generated by the compiler).

> Hashtable<String, ItemInfo> userCart = (Hashtable)
> session.getAttribute("userCart");
[quoted text clipped - 6 lines]
> what's wrong with this sentence?
> how can I fix it?

Some other posters have commented on using a Map instead of Hashtable. It  
would be a good idea, when map is initially created, to use  
Collections.checkedMap - which will ensure that the contents are what you  
expect.

Map<String, ItemInfo> cart=Collections.checkedMap(
    Collections.synchronizedMap(new HashMap()),
    String.class,
    ItemInfo.class));

Now, you can safely assume that the contents of your map are always  
String, ItemInfo. The compiler will still generate the same warning, which  
you can now safely turn off by decorating the calling method with

@SuppressWarnings("unchecked")

Note that this turns off all unchecked cast warnings in that method, so do  
make sure that you have taken reasonable precautions to externally protect  
your data before turning off the warning.

HTH,

-Zig
JTL.zheng - 01 Nov 2007 16:14 GMT
Thanks very much for all of you.

I am using cool_guy's way which define a new class.

Roedy Green's
HashMap<String, ItemInfo> userCart = (HashMap<String, ItemInfo>)
session.getAttribute("userCart");
it still get a warning:

Zig's
Map<String, ItemInfo> cart=Collections.checkedMap(
       Collections.synchronizedMap(new HashMap()),
       String.class,
       ItemInfo.class));

eclipse change it to:

Map<String, ItemInfo> cart = Collections.checkedMap(Collections
                .synchronizedMap(new HashMap<String, ItemInfo>()), String.class,
ItemInfo.class);

Thanks again.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.