Machine-translated by Google. Questions are authored in English.
Top

Discussion

Which of these correctly declares an atomic counter?

  • A.AtomicInteger hits = new AtomicInteger();
  • B.atomic int hits = 0;
  • C.volatile AtomicInteger hits;
  • D.AtomicInteger hits = new AtomicInteger(0);

Answer: D

AtomicInteger is an ordinary class taking an optional initial value, and both the zero-argument and int-argument constructors exist, so two of these compile. The one shown is the clearer of the pair because it states the starting value. Java has no atomic keyword.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.