Avatar of cofactor
cofactor
 asked on

how to get rid of this Compile warning

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

Compile warning attached.

how to get rid of this Compile warning ?
error.png
JavaJSPJava EE

Avatar of undefined
Last Comment
CEHJ

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Mick Barry

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
CEHJ

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

Open in new window

Mick Barry

best you can do is add an annotation to ignore the warning. personanlly not a fan of doing that.
CEHJ

Sorry - missed the code posted
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
cofactor

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

This does not remove the warning.

Mick Barry

> 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
for_yan

⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
CEHJ

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
SOLUTION
for_yan

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
cofactor

ASKER
>>@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 Barry

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.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Mick Barry

so its telling the compiler to ignore all warnings of type "unchecked", and to stop displaying warnings about them.
SOLUTION
CEHJ

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.