• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

when do we use generic type and wild cards

 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello pals,
I am really very confused with the generics type part.
When do we use <?>/<?extends Object> <?> always gives an error as it is an unknown type.so I have to use <T> instead of wild card character.
K& B also says to use <T> instead of <?>.
Then how do i deal with the question using <?>.this part is really tricky.

Please help



thanks
[ August 30, 2008: Message edited by: Bear Bibeault ]
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It depends when and where you are using it. If you a creating a generic method then <T> defines the generic type in that method. If you are defining a generic collection then <?>, <String> is used. I've not touched generics since I took the SCJP so correct me if I am wrong.

Post a code snippet of what you are trying to do. It may help answer you question a bit better.
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

The rule is :

1) Question mark works when declaring a reference for a variable, and not for generic class and method declarations.

example of illegal use of ?

class xyz<?> { }

class xyz<? extends Object> { }

public <? extends Number> void disc(T t) // Method declaration inside a class

Legal use of ? is in reference variable like mainly in collections.

public void disc(List<? extends Object> mylist) { } // Here mylist is an reference variable

public void disc(List<? extends Serializable> mylist) { } // here Serializable is interface.

List<?> list = new LinkedList<car>(); // (1)
List<? extends Object> list = new LinkedList<car>(); // (2)

Above statement 1 and 2 both are same in meaning

List<? super PassengerCar> mylist = new Vector<Vehicle>();
 
Shashank Sharma
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks
 
Sheriff
Posts: 9692
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is a reason for ? not working in class declaration. The rule is that if you are not going to declare any reference of the type of the generic parameter, then you can use ? instead of T(or infact any identifier can be used instead of T). But if you declare a class as MyClass<?> then what is the use??? You cannot say this

class MyClass<?>
{
? obj;//not allowed
}

So you must declare it as

class MyClass<A>
{
A obj;//OK
}

but for method parameters and collections, you can use ? if you don't want to create any reference of the type of the parameter or collection.

void hello(List<?> list)
{
}

you could have also used T instead of ?. But if you want to create a reference of the generic type, you must use an identifier instead of ?

<T> void hello(List<T> list)
{
T obj = list.get(0);
}

you cannot write the above code as

<?> void hello(List<?> list)
{
? obj = list.get(0);//? not allowed like this
}
 
Looky! I'm being abducted by space aliens! Me and this tiny ad!
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic