import
java.util.concurrent.atomic.AtomicInteger;
public class AtomicCounter {
private AtomicInteger c = new AtomicInteger(0);
public void increment() {
// insert code here
}
}
Which line of code, inserted inside the increment () method, will
increment
the value of c?
A. c.addAndGet();
B. c++;
C. c = c+1;
D. c.getAndIncrement ();
Risposta
D
A
Questo codice non compila perché non viene passato l’obbligatorio
parametro di ingresso :
The
method addAndGet(int) in the type AtomicInteger is not applicable for the
arguments ()
Dovrebbe essere c.addAndGet(1);
.
B
L’operatore postifix (++) si applica solo agli interi e quindi il codice
non compila essendo c un AtomiInteger .
Type
mismatch: cannot convert from AtomicInteger to int
C
L’operatore somma (+) non è applicabile al tipo
AtomicInteger.
The
operator + is undefined for the argument type(s) AtomicInteger, int
D
Questo è il modo corretto di incrementare un
AtomicInteger di 1 .
Riferimenti :
Nessun commento:
Posta un commento