Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Xlint:unchecked

 
ahsan mir
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 809
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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 ]
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please do not crosspost among several forums.
You've already posted this in the SCWCD forum.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic