class array in jdk1.5

C

cell

Dear all,

I have an old java programmer using jdk 1.4.x.

After I update the jdk to 1.5, a warning is checked out.

The original line is:
Class[] cls={String.class,String.class,Integer.class};
and the warning is:
Class is a raw type. References to generic type Class<T> should be
parameterized.

However, I cannot solved it. I have tried
Class<Object>[] cls={String.class,String.class,Integer.class};
it does not work.

Could anyone help me?

Thanks!
 
A

Arne Vajhøj

cell said:
I have an old java programmer using jdk 1.4.x.

After I update the jdk to 1.5, a warning is checked out.

The original line is:
Class[] cls={String.class,String.class,Integer.class};
and the warning is:
Class is a raw type. References to generic type Class<T> should be
parameterized.

However, I cannot solved it. I have tried
Class<Object>[] cls={String.class,String.class,Integer.class};
it does not work.

Try:

Class<?>[] cls = ...

Arne
 
L

Lew

cell said:
The original line is:
Class[] cls={String.class,String.class,Integer.class};
and the warning is:
Class is a raw type. References to generic type Class<T> should be
parameterized.

However, I cannot solved it. I have tried
Class<Object>[] cls={String.class,String.class,Integer.class};
it does not work.
Try:

Class<?>[] cls = ...

Brian Goetz's excellent articles, "Going Wild with Generics", Part 1
<http://www.ibm.com/developerworks/java/library/j-jtp04298.html>
and Part 2
<http://www.ibm.com/developerworks/java/library/j-jtp07018.html>
explain about generics wildcards.

Joshua Bloch's seminal /Effective Java/ has an entire chapter on generics,
available for free at
<http://java.sun.com/docs/books/effective/generics.pdf>
that explains a lot.

The Sun generics tutorial,
<http://java.sun.com/docs/books/tutorial/extra/generics/index.html>
which of course you've already read or you wouldn't be embarking on this
adventure, has a chapter on wildcards,
<http://java.sun.com/docs/books/tutorial/extra/generics/wildcards.html>

Beware, generics and arrays do not play well together. Instead of
'Class<?>[]' I'd consider 'List <Class <?>>'.

Yes, there's an array under the hood in some 'List' implementations, but at
least the API hides the messiness for you.

Anyway, read the referenced links. It's the minimum to get you competent, or
at least start to, with respect to generics.
 
J

Joshua Cranmer

cell said:
However, I cannot solved it. I have tried
Class<Object>[] cls={String.class,String.class,Integer.class};
it does not work.

In general, a raw type is most accurately replaced with the wildcard
type: Foo ~= Foo<?>, not Foo<Object>.

In short, the problem is that a Class<String> is not a Class<Object>.
The canonical example of why this would be bad is this:

List<String> ls = new ArrayList<String>();
List<Object> lo = ls;
lo.add(1); // Integers are Objects, right?
String arg = ls.get(0); // Oops!

Wildcards alleviate this through various subtyping rules. Basically, a
List<?> would prohibit you from adding an Integer to the list but would
allow you to get an Object from the list.
 

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,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top