1

I am learning Java. I have task to implement HashDictionary using DictionaryInterface

Hashtable<K,V> h;
Set<K> s = h.keySet();
K elements[]=(K[])Array.newInstance(KeyType, h.size());

When I have written the above statements the Eclipse IDE is showing a warning message.

@SuppressWarnings("unchecked")

If I add the above message before start of a method. The warning is disappeared. What is this means. Can anyone give the reason? Thanks in Advance

user2454830
  • 193
  • 2
  • 2
  • 11
  • Before you ask, please follow this guideline: http://meta.stackexchange.com/questions/156810/stack-overflow-question-checklist – Jonathan Root Oct 09 '13 at 19:50

3 Answers3

3

Warning in general are just that - warnings. Things that can easily go wrong, but havent technically gone wrong yet. Sometimes you are well aware of them and can simply ignore them. In those cases you might want to use @SuppressWarnings("unchecked"). That will signal the compiler to no post its warning, thus giving you an absolutely clean compile.

Note that even if you dont put that there and you do get the warning your program can still work just fine. Its just that there is an elevated chance that your program will break at that point.

If you want, you can also remove the warning entirely from eclipse by going to Windows > Preferences > General > Editors > Text Editors > Annotations > Warnings > Annotation types and then select the warnings you do/dont want.

David says Reinstate Monica
  • 19,209
  • 22
  • 79
  • 122
2

The annotation type SuppressWarnings supports programmer control over warnings otherwise issued by the Java compiler. It contains a single element that is an array of String. If a program declaration is annotated with the annotation @SuppressWarnings(value = {someString}), then a Java compiler must not report any warning identified by one of someString if that warning would have been generated as a result of the annotated declaration or any of its parts.

For example, Unchecked warnings are identified by the string unchecked. In your example, the below statement will emit an unchecked warning:

K elements[]=(K[])Array.newInstance(KeyType, h.size());

This statement contains an unchecked conversion, as the compiler doesn't know if the cast is safe or not; and will simply emit an unchecked conversion warning. Which can be suppressed by using @SuppressWarnings("unchecked") annotation.

Debojit Saikia
  • 10,532
  • 3
  • 35
  • 46
1

In case you don't already know (you said you're a beginner), Eclipse often tells you what is causing the warning and can sometimes give you suggestions on how to fix it.

Of course, it's always much better to fix the warnings than to suppress them (unless you're doing really intricate stuff). Hope this helps.

huwr
  • 1,720
  • 3
  • 19
  • 34