tags:

views:

132

answers:

3
+1  Q: 

Generics in Java

Having considered the official Java tutorial and some threads from the past, few things remain unclear and so I'd be glad if someone would explain it to be in plain words.

Class valueClass = agp.getValueClass(); ///the method's return type is Class<?>

if(Double.class.equals(valueClass))
{
...
}

With that my IDE shows me a raw type warning. I've changed the first line to

 Class<?> valueClass = agp.getValueClass();

What other reasonable alternatives do I have and what effects do they bring?

Thank you in advance.

+4  A: 

There is one alternative:

Double.class.isAssignableFrom(valueClass)

It does not only check valueClass for equality, but also 'allows' subclasses of Double.


In addition, your agp variable probably holds the value as well. Assuming there is a getValue() method, which probably returns the type Object, you could use instanceof:

if(agp.getValue()!=null){
  if(agp.getValue().getClass() instanceof Double){ //getClass() returns the runtime class
    //it's a (subclass of) Double!
  }
}
f1sh
The return value of the mentioned method is not mandatorily a Double
kerrl
In that case, the method i stated will return false. Are you looking for a different behaviour?
f1sh
I am looking for a different generic, an alternative to the '<?>' that might make more sense.Also, a brief explanation of the used generic would be great ;-)
kerrl
If it's about that, you can always specify the most common superclass of all the different types that your `value` can be, probably it's Object. In that case, use `Class<? extends Object>`.
f1sh
+1  A: 

My IDE shows me a raw type warning.

You must give your local variable the same type that the method returns. Since the method returns Class<?>, that's what you have to use. There is no way to limit the generic parameter at this point.

Aaron Digulla
What exactly does the '<?>' mean? In my own words it might be just any class.To what extend does '<? extend Object>' differ to '<?>' ?
kerrl
It means "anything". `<? extends Object>` means "anything that extends `Object`". They are not the same thing as far as the compiler is concerned because the two "anythings" could be different anythings.
Aaron Digulla
The `?` is a placeholder for the encapsulated type. `Class<?>` basically means "any type". `Class<? extends Object>` basically states the encapsulated type is/must be a subclass of `Object`. That is trivial, since every type subclasses from `Object`, but your IDE will still show a warning if you dont write it.
f1sh
A: 

Thank you all, now I'm clear on that :-)

With

Class<?> 

it can be just any class, even Object itself, whereas with

Class<? extends Object> 

only a subtype of Object (thus not Object itself) is accepted.

kerrl
Wrong! `Class<? extends Object>` can also hold a `Class<Object>`. In other words `extends Object` **includes** `Object`. They are pretty much equivalent.
Joachim Sauer
pretty much, is there any difference at all?
kerrl
@kerrl - no, no difference at all. Just like when you define a class, you can explicitly say `extends Object` if you like but it makes no functional difference. In fact, in my IDE (IntelliJ) you get a yellow squiggly warning that "... '? extends Object' is redundant, consider replacing with '?'".
Andrzej Doyle
@kerrl: they are different in the (rather dangerous) case that you have a class with the simple name `Object` that isn't `java.lang.Object`. Granted, that's mostly nit-picking.
Joachim Sauer