• Post Reply Bookmark Topic Watch Topic
  • New Topic

cast warning  RSS feed

 
Alan Shiers
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,

I need to get rid of a warning message regarding a cast. See following:

ObjectInputStream ois = new ObjectInputStream(client.getInputStream());
...
ClientServerContainer<String, Long> csc = (ClientServerContainer<String, Long>)ois.readObject();

For clarity ClientServerContainer<K,V> extends Hashtable<K,V>. The warning message I get is: Type safety: Unchecked cast from Object to ClientServerContainer<String,Long>. What should I be doing to get rid of the warning so that this compiles properly?

Alan
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can add the @SupressWarnings("unchecked") annotation just before the cast. This tells the compiler "I know that this is unchecked, but I also know it will have the right data in it.

Of course, you should be sure that what comes out of the object input stream is actually what you expect.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!