my dog learned polymorphism*
The moose likes Beginning Java and the fly likes Xlint:unchecked Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Xlint:unchecked" Watch "Xlint:unchecked" New topic
Author

Xlint:unchecked

ahsan mir
Ranch Hand

Joined: Jul 17, 2006
Posts: 42
Hello,
when i try to compile the following code as part of a (servlet )application , the following Xlint:unchecked error occurs.I cant understand since I am using jdk1.5 or is it referring to generics in collections?if so could anyone pls reply how to fix it(inc any code help).Thanks in advance.

****************************************************************************
import java.util.*;

public class BeerExpert{
public List getBrands(String color) {
List brands = new ArrayList();
if(color.equals("amber"))

{
brands.add("Jack Amber");
brands.add("Red Moose");
}
else
{
brands.add("Jail Pale Aile");
}
return (brands);
}

}

**************************************************************************
C:\javac BeerExpert.java
Note: BeerExpert.java uses unchecked or unsafe operations.
Note :Recompile with -Xlint:unchecked for details
--------------------------------------------------------------------------------
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809

Originally posted by ahsan mir
when i try to compile the following code as part of a (servlet )application , the following Xlint:unchecked error occurs.I cant understand since I am using jdk1.5 or is it referring to generics in collections?if so could anyone pls reply how to fix it(inc any code help).Thanks in advance.



First of all, its not an error. Its just a warning. Still you can run your servlet.

Compile your java class by javac -Xlint:unchecked Test.java You will get details of this warning.

The reason is because you are doing unsafe operation by adding String objects in an unsafe list brands.

Replace your line List brands=new ArrayList(); with

List<String> brands=new ArrayList<String>();

This brands only take String. No other object can be stored in list.

More on generics...

View the very first link (pdf) of google search

What is Java Generics

Naseem
[ July 17, 2006: Message edited by: Naseem Khan ]

Asking Smart Questions FAQ - How To Put Your Code In Code Tags
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

Please do not crosspost among several forums.
You've already posted this in the SCWCD forum.


[My Blog]
All roads lead to JavaRanch
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

If this is part of a Servlet, then it doesn't have to do with the SCJP exam. As pointed out above, the real issue here is getting comfortable with generics, so I'll move this to Java in General (beginners) for you.
[ July 17, 2006: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
 
GeeCON Prague 2014
 
subject: Xlint:unchecked