how to solve this warning in Eclipse?

B

Brandon McCombs

I use multiple vectors in certain parts of my program and I get the
following warning when adding stuff to the vector. Can anyone tell me
what it means and give an example of how I can get rid of the warnings?


Type safety: The method addElement(Object) belongs to the raw type
Vector. References to generic type Vector<E> should be parameterized

thanks
 
R

Rhino

Brandon McCombs said:
I use multiple vectors in certain parts of my program and I get the
following warning when adding stuff to the vector. Can anyone tell me what
it means and give an example of how I can get rid of the warnings?


Type safety: The method addElement(Object) belongs to the raw type Vector.
References to generic type Vector<E> should be parameterized
I assume you are using a Java 1.5 (or 1.6) compiler; Java 1.5 is the version
that introduced 'generics'.

The basic idea of generics is to introduce several features that encourage
type safety in your code. Type safety has been present in languages like C++
for years but were largely missing from Java until 1.5. I can't explain
generics very well but there are some articles about it in the 1.5
documentation. Look for it under the list of features that are new to 1.5.

Basically, to stop your warnings, you need to tell Java what sort of Object
you are going to store in your Vectors. For example, if you are putting
Strings in your Vector, use this:

Vector<String> myVector = new Vector<String>();
 
B

Brandon McCombs

Rhino said:
I assume you are using a Java 1.5 (or 1.6) compiler; Java 1.5 is the version
that introduced 'generics'.

The basic idea of generics is to introduce several features that encourage
type safety in your code. Type safety has been present in languages like C++
for years but were largely missing from Java until 1.5. I can't explain
generics very well but there are some articles about it in the 1.5
documentation. Look for it under the list of features that are new to 1.5.

Basically, to stop your warnings, you need to tell Java what sort of Object
you are going to store in your Vectors. For example, if you are putting
Strings in your Vector, use this:

Vector<String> myVector = new Vector<String>();

Hey Rhino,

Thanks for your help on that last one. I'm in the middle of fixing all
those warnings. Your code helped.

Another thing I ran into is something similar but with Hashtables. I get
Type safety: The method put(Object, Object) belongs to the raw type
Hashtable. References to generic type Hashtable<K,V> should be parameterized

Is the fix similar for this as it was for the Vector?
 
B

Brandon McCombs

Rhino said:
I assume you are using a Java 1.5 (or 1.6) compiler; Java 1.5 is the version
that introduced 'generics'.

The basic idea of generics is to introduce several features that encourage
type safety in your code. Type safety has been present in languages like C++
for years but were largely missing from Java until 1.5. I can't explain
generics very well but there are some articles about it in the 1.5
documentation. Look for it under the list of features that are new to 1.5.

Basically, to stop your warnings, you need to tell Java what sort of Object
you are going to store in your Vectors. For example, if you are putting
Strings in your Vector, use this:

Vector<String> myVector = new Vector<String>();

I have one last line that I can't fix the warning for.

allAttribsAndVals.get(i).add( newField.getText() );

allAttribsAndVals is declared as Vector<Vector> and the Vectors that it
holds are declared as Vector<String>. No matter what I do I can't get
the warning to go away. Any ideas?
 
R

Rhino

Brandon McCombs said:
Hey Rhino,

Thanks for your help on that last one. I'm in the middle of fixing all
those warnings. Your code helped.

Another thing I ran into is something similar but with Hashtables. I get
Type safety: The method put(Object, Object) belongs to the raw type
Hashtable. References to generic type Hashtable<K,V> should be
parameterized

Is the fix similar for this as it was for the Vector?

Yes, it's the same idea as for Vectors except that you need two parameters
in the angle brackets; the first one tells you the type of the Key (K) and
the second one tells you the type of the Value (V). So if you have a
HashTable where the Key is an Integer and the Value is a String, you'll need
something like this:

HashTable<Integer, String> myHashTable = new HashTable<Integer, String>();
 
R

Rhino

Brandon McCombs said:
I have one last line that I can't fix the warning for.

allAttribsAndVals.get(i).add( newField.getText() );

allAttribsAndVals is declared as Vector<Vector> and the Vectors that it
holds are declared as Vector<String>. No matter what I do I can't get the
warning to go away. Any ideas?

Well, you haven't specified the text of the warning you are getting for this
line so that makes it a little hard ;-)

Looking at the API for Vector, it seems that this version of the add()
method is expecting a value of type 'E' but I'm still rocky on the notation
so I'm not completely sure what 'E' means in this context. You haven't said
what type 'newField' is but I'm guessing that its a JTextField or JTextArea,
which means it is using the getText() method from JTextComponent, which
returns a String: that should work fine. But there are many other getText()
methods, some of which don't return Strings. Look up the API for the type of
'newField' then see what it's getText() method is returning; if it's not a
String, that is likely your problem. Ultimately, I think the add() method
will have to pass a String to the Vector otherwise you'll continue to the
get the warning.

If newField.getText() _is_ returning a String, I'm not sure what the problem
is. In that case, perhaps someone else with better understanding of Generics
can help you.

Or maybe you'd like to wade through the Generics tutorial yourself so that
you can figure it out on your own? The main Generics tutorial is at
http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf but parts of it are
over my head since my theory isn't that strong. I think there's a more basic
tutorial as well but I don't remember where I saw it.
 
J

John C. Bollinger

Brandon said:
allAttribsAndVals.get(i).add( newField.getText() );

allAttribsAndVals is declared as Vector<Vector> and the Vectors that it
holds are declared as Vector<String>. No matter what I do I can't get
the warning to go away. Any ideas?

Declare allAttribsAndVals as as type Vector<Vector<String>>.
 
J

John C. Bollinger

Brandon said:
Another thing I ran into is something similar but with Hashtables. I get
Type safety: The method put(Object, Object) belongs to the raw type
Hashtable. References to generic type Hashtable<K,V> should be
parameterized

Is the fix similar for this as it was for the Vector?

Yes.

You might want to read
http://java.sun.com/j2se/1.5.0/docs/guide/language/generics.html for a
brief introduction to generics. There is also a link there to a
lengthier, tutorial-style writeup on generics that you may find useful.
 
B

Brandon McCombs

John said:
Declare allAttribsAndVals as as type Vector<Vector<String>>.

Thanks John. I didn't know I could nest them like that. It worked just
fine after that.
 
J

John C. Bollinger

Brandon: cover your ears. What follows is likely to confuse you more
than it helps you.
The basic idea of generics is to introduce several features that encourage
type safety in your code. Type safety has been present in languages like C++
for years but were largely missing from Java until 1.5. I can't explain
generics very well

[...]

I'm afraid I have to agree with you on that last part.

Generics are not *about* type safety. They are *about* extending the
Java type system with parameterized types. Use of such types can
greatly reduce the need to perform type conversions that cannot be
proved valid by the compiler; this is what most people mean when they
talk about "type safety" with respect to Generics. Type safety in that
sense is an important application of parameterized types, but not the
central idea.

C++ has had parameterized types ("templates") for a long time, but its
type system is overall much weaker than Java's, thus, in a broader sense
of the term, it has poorer type safety than Java does now or did before
the introduction of generics.
 
T

Timo Stamm

John said:
Generics are not *about* type safety. They are *about* extending the
Java type system with parameterized types. Use of such types can
greatly reduce the need to perform type conversions that cannot be
proved valid by the compiler; this is what most people mean when they
talk about "type safety" with respect to Generics. Type safety in that
sense is an important application of parameterized types, but not the
central idea.


Java generics can only be used for type safety because the type info is
erased. What else can you use java generics for?


Timo
 
R

Roedy Green

Java generics can only be used for type safety because the type info is
erased. What else can you use java generics for?

I think it would be more accurate to say that Java Generics can be
used to check type consistency EVEN THOUGH the type information is
erased before run time. Generics are a purely compile time check.

They also help the compiler to generate more efficient code. Generics
give the code generator hints when it can presume something more
specific than Objects being tossed about.
 
J

jlowery05

Since the type information is 'compiled out', I can't see how Generics
would allow the compiler to generate more efficient code. I've reverse
compiled some Generic constructs and they look pretty much identical to
their non-Generic cousins.
 
I

Ian Shef

Since the type information is 'compiled out', I can't see how Generics
would allow the compiler to generate more efficient code. I've reverse
compiled some Generic constructs and they look pretty much identical to
their non-Generic cousins.

My understanding is that without Generics, the compiler sometimes must insert
casts and type checks to perform verification at runtime.
With Generics, some of these casts and type checks can be eliminated because
the checking has been performed at compile time.

I am still learning Generics (there is a lot to learn), so I hope that I got
this right.
 
T

Timo Stamm

Ian said:
My understanding is that without Generics, the compiler sometimes must insert
casts and type checks to perform verification at runtime.
With Generics, some of these casts and type checks can be eliminated because
the checking has been performed at compile time.

No, the information is erased at compile time. From the java generics
tutorial:

| Basically, erasure gets rid of (or erases) all generic type
| information. All the type information betweeen angle brackets is
| thrown out, so, for example, a parameterized type like List<String> is
| converted into List. All remaining uses of type variables are replaced
| by the upper bound of the type variable (usually Object). And,
| whenever the resulting code isn’t type-correct, a cast to the
| appropriate type is inserted [...]



Timo
 
C

Chris Smith

Timo Stamm said:
Java generics can only be used for type safety because the type info is
erased. What else can you use java generics for?

By far the most important use is to communicate to other developers
using an API the exactly type of object they need to use to interact
with your classes. Generic type parameters are described by JavaDoc,
recognized and used by "auto-complete" features of development
environments, etc. This really doesn't fit under "type safety" so much
as convenience. As John said, though, it is described as a benefit of
extending the type system.

I'm saying this is a local benefit. It should not be taken to imply
that I agree generics are a net benefit to the language. I think the
jury is still out on that one.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top