What is the meaning of the <?>
token in this code copied from www.JavaPractices.com? When I replace it with the more conventional looking <T>
used for generic types, it fails to compile. (Error: T cannot be resolved to a type.) Why?
// <?> occurs 3 times in the entire program. When it is replaced with <T> the
// program no longer compiles.
void activateAlarmThenStop()
{
Runnable myPeriodicTask = new PeriodicTask();
ScheduledFuture<?> soundAlarmFuture =
this.executorService.scheduleWithFixedDelay(myPeriodicTask,
startT,
period,
TimeUnit.SECONDS
);
Runnable stopAlarm = new StopAlarmTask(soundAlarmFuture);
this.executorService.schedule(stopAlarm, stopT, TimeUnit.SECONDS);
}
private final class StopAlarmTask implements Runnable
{
StopAlarmTask(ScheduledFuture<?> aSchedFuture)
{
fSchedFuture = aSchedFuture;
}
public void run()
{
CConsole.pw.println("Stopping alarm.");
fSchedFuture.cancel(doNotInterruptIfRunningFlag);
executorService.shutdown();
}
private ScheduledFuture<?> fSchedFuture;
}
Edit: Of course when we use generic type tokens like <T>
, it has to appear in the class declaration. Here there is no <T>
nor <?>
in the class declaration but it still compiles and runs properly.
A token is the smallest element of a program that is meaningful to the compiler. Tokens can be classified as follows: Keywords. Identifiers. Constants.
Tokens in java include identifiers, keywords, literals, operators and, separators.
Java tokens are the different elements of the Java program which are identified by the compiler and separated by delimiters. The Java tokens are not part of the delimiters. A token is the tiniest element of a compiler-significant program. The compiler breaks lines into pieces of text known as Java tokens.
Tokenizing int as a literal: it is a keyword, that happens to name a type in Java. Tokens like 1 are literals whose type is int; the token int is a keyword. Tokenizing "Hi" as two separators with the identifier Hi in between: it is a single String literal.
It fails to compile, because your class is not generic (nor any of your methods). In this particular example joker (?) means that ScheduledFuture may be parametrized by anything.
Sometimes, there is no sense to make the whole class generic if you use another generic class inside and you don't know the exact type that will be used. In this example you had three options:
< ? >
) - it allows to retrieve anything as a result of FutureResult (String, Integer, your custom class). You can also narrow the scope of a possible generic type into some subclasses, for example ScheduledGeneric< ? extends MyObject >
or into superclasses: ScheduledGeneric< ? super MyObject >
This is an example of using wildcard in a type argument. i.e. a generic type.
A wildcard parameterized type is an instantiation of a generic type where at least one type argument is a wildcard. Examples of wildcard parameterized types are Collection<?>
, List<? extends Number>
, Comparator<? super String>
and Pair<String,?>
.
A wildcard parameterized type denotes a family of types comprising concrete instantiations of a generic type. The kind of the wildcard being used determines which concrete parameterized types belong to the family.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With