File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Why am I getting this error? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why am I getting this error?" Watch "Why am I getting this error?" New topic

Why am I getting this error?

KiranKumar Gogineni

Joined: May 23, 2006
Posts: 9
I found this code on the net and compiled using JDK 5.0 on my system. Why am I getting this error?
This is the error text:
Note: uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

This is the code:

import java.util.*;
public class VectorTest
static void search(Vector v, Object o)
int idx = v.indexOf(o);
System.out.print(o + " ");
if (idx < 0) System.out.println("not found");
else System.out.println("found in position " + idx);
public static void main(String[] args)
{ // Build Vector:
Vector v = new Vector();
v.addElement(new Integer(88));
v.addElement(new Integer(17));
v.addElement(new Integer(-10));
v.addElement(new Integer(34));
v.addElement(new Integer(27));
v.addElement(new Integer(0));
v.addElement(new Integer(-2));
// Search:
search(v, new Integer(0));
search(v, new Integer(1));
Paul Santa Maria
Ranch Hand

Joined: Feb 24, 2004
Posts: 236
Because "Vector" is an old Java 1.0 container that was deprecated a long, long time ago. Please consider using ArrayList instead.

Paul M. Santa Maria, SCJP
marc weber

Joined: Aug 31, 2004
Posts: 11343

These are just warnings that you are not specifying a generic type for your List (the Vector). It does not have to do with Vector being an old class -- you would get the same warnings using an ArrayList.

See this article on Generics.

"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
Paul Santa Maria
Ranch Hand

Joined: Feb 24, 2004
Posts: 236
OK Marc - you're right. That's what I get for not actually typing in the code and test compiling myself ;-)

Nevertheless, using "Vector" is discouraged, and using "ArrayList" is preferred.

And if you're using Java 5.0 ... and if you're sure that all of your *end users* are ALSO using the Java 5.0 JRE ... then generics are preferred over ArrayList. Otherwise (as you explained) you have to cast. And that cast (as Xlint shows) is precisely the thing javac is complaining about.

Here are some other links on the "unchecked or unsafe operations" warning:

You're supposed to be able to enable/disable specific lint warnings like this:
javac -Xlint:unchecked # give details of any "unchecked" warnings
javax -Xlint:-unchecked # disable "unchecked warnings"
javac -Xlint:all # Enable all Lint warnings

Instead of sacrificing backward compatibility (if some of your users might be running JRE 1.4.2 or earlier), you can also do this:
javac -source 1.4 -target 1.4

'Hope that helps!
marc weber

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Paul Santa Maria:
...Nevertheless, using "Vector" is discouraged, and using "ArrayList" is preferred...

Absolutely. Vector is basically "legacy" now -- kept alive so that old code isn't broken. But (in general) you should not write new code using Vectors.
I agree. Here's the link:
subject: Why am I getting this error?
It's not a secret anymore!