Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the meaning of the <?> token in Java?

Tags:

java

generics

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.

like image 304
H2ONaCl Avatar asked Aug 04 '11 08:08

H2ONaCl


People also ask

What does token mean in Java?

A token is the smallest element of a program that is meaningful to the compiler. Tokens can be classified as follows: Keywords. Identifiers. Constants.

What are the 4 tokens of Java?

Tokens in java include identifiers, keywords, literals, operators and, separators.

How do tokens work in Java?

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.

What is integer token in Java?

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.


2 Answers

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:

  1. make StopAlarmTask generic (there is no sense in this case)
  2. use concrete type in ScheduledFuture, but then it would be only one possible result type, for example String or Integer
  3. use wildcard (< ? >) - 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 >
like image 187
omnomnom Avatar answered Nov 05 '22 13:11

omnomnom


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.

like image 42
A. K. Avatar answered Nov 05 '22 14:11

A. K.