File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why am I getting this error?" Watch "Why am I getting this error?" New topic
Author

Why am I getting this error?

KiranKumar Gogineni
Greenhorn

Joined: May 23, 2006
Posts: 9
Hi,
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: VectorTest.java 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));
System.out.println(v);
// 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
Sheriff

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
sscce.org
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:
http://forum.java.sun.com/thread.jspa?threadID=584311&messageID=3009290
http://www.myjavaserver.com/exec/ZitoYetpKLwzNf2CZvwBFfNjLDwyZnxzT9vB1j3BM1JBPfwBMuwBVHwp0f2y

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
Sheriff

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: http://aspose.com/file-tools
 
subject: Why am I getting this error?
 
Similar Threads
Stuck with KMeans Clustering Algorithm
vector
What about the little guy?
problem in vector