We help IT Professionals succeed at work.

how to get rid of this Compile warning

cofactor
cofactor asked
on
353 Views
Last Modified: 2012-06-27
code:
Map<String,String> aVar=(LinkedHashMap<String,String>)session.get("aVar");

Compile warning attached.

how to get rid of this Compile warning ?
error.png
Comment
Watch Question

Java Developer
CERTIFIED EXPERT
Top Expert 2010
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT
Top Expert 2016

Commented:
Genericise your Map declaration, e.g.
Map<String, String> m = new LinkedHashMap<String, String>();

Open in new window

Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
best you can do is add an annotation to ignore the warning. personanlly not a fan of doing that.
CERTIFIED EXPERT
Top Expert 2016

Commented:
Sorry - missed the code posted

Author

Commented:
>>>Map<String,String> aVar=(Map<String,String>)session.get("aVar");

This does not remove the warning.

Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
> This does not remove the warning.

Didn't say it would :)  You can't get rid of that warning as the compiler has no way of know what type is being returned by get()

all you do is tell the compiler to ignore it (and not display it) using an annotation
Awarded 2011
Awarded 2011

Commented:
CERTIFIED EXPERT
Top Expert 2016

Commented:
You can't remove the warning other than by compiler settings. Take comfort in the fact that if you compile the JDK source, you'll get many more warning. Generics is a shoehorned fit
Awarded 2011
Awarded 2011
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
>>@SuppressWarnings("unchecked")

when do you write  "unchecked"  warnings ?  Could you please list out few examples ?

I think , this keyword is more appropriate for Exceptions where "checked" is for predefined exceptions. e.g SQLException when you trying to connect to DB .

And "unchecked" is for Exceptions which can not be predicted at compile time . That is for run time. ..e.g RuntimeException.

Here  you are using in the context of warnings ...thats the reason  I'm worried.

when do you write  "unchecked"  warnings  ?
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
Thats what I mentioned above about telling the compiler to ignore those warnings
Not a good idea imo to be hiding warnings

unchecked is referring to the unchecked cast that it is warning you about.
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
so its telling the compiler to ignore all warnings of type "unchecked", and to stop displaying warnings about them.
CERTIFIED EXPERT
Top Expert 2016
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.