• Post Reply Bookmark Topic Watch Topic
  • New Topic

Type safety: Unchecked cast from Object to List<String>  RSS feed

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone help?

When I changed my code from using arrays

final Offer[] daoOffers = (Offer[])daoManager.executeAndClose(new DAOCommand() {
public Object execute (DAOManager manager) {
return manager.getOfferDao().findOffersByCampaign(daoCampaign);
}
});


to

final List<Offer> daoOffers = (List<Offer>)daoManager.executeAndClose(new DAOCommand() {
public Object execute (DAOManager manager) {
return manager.getOfferDao().findOffersByCampaign(daoCampaign);
}
});


I get the following warning message :
Type safety: Unchecked cast from Object to List<Offer>

Is there something wrong with my code? It's just a warning message but I would like to understand why..
 
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you use jdk1.5 or above Compiler expect you to Cast an Object safely . Since Compiler do not have any idea about the object type ,it warns you . if you want to ignore the warning use @SuppressWarnings("unchecked") .
 
Bartender
Posts: 4002
42
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


What does "executeAndClose" method return? Is it of type Object ? Then it should be List<>.
 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Masrudyn Main,

The reason is simple.
The problem is that a cast is a runtime check - but due to type erasure, at runtime there's actually no difference between a HashMap<String,String> and HashMap<Integer, Integer>.

Warnings are just for safety purpose, if you are confident about your code you can simply ignore.

 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi tanzy,

welcome to javaranch. we are happy to have you here
 
Tanzy Akhtar
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks seetharaman venkatasamy.
 
Masrudyn Main
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys for all your replies.. actually for both instances I did do a cast:

final Offer[] daoOffers = (Offer[])daoManager.executeAndClose(new DAOCommand() {

final List<Offer> daoOffers = (List<Offer>)daoManager.executeAndClose(new DAOCommand()

But somehow the compiler does not like it when I am cast List<Offer>. There's no problem when I cast Offer[].

I'm using JDK 1.6. My workaround is for my "executeAndClose" method to return Offer[] and then convert the Offer[] to List<Offer> but I would like to know why. It seems that when I am returning a List, the compiler does not like it?
 
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your question has already been answered. The compiler claims because it can't be sure if that cast will work at runtime (although it will). It also depends in the types used by your persistence API. Hibernate, for example, uses raw types lists for compatibility with JDK 1.4-, so whenever you return a value to a parametrized list the compiler complains.
 
Masrudyn Main
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I'm much clearer now. Thanks so much everyone!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!