Sunday, September 16, 2007

Pop Quiz: AtomicInteger equals method

What does this print?

int i = 100;
AtomicInteger one = new AtomicInteger(i);
AtomicInteger two = new AtomicInteger(i);
System.out.println( one.equals(two) );


akash said...


Dennis Byrne said...

Nope. Try again.

CARFIELD said...

From the API document, it inherence equals() method from java.lang.Object. So it probably do the samething as what Object.equals() does...

If the class define it own equals, it probably have it own equals() method show at the API document

And I guess, we should already read the API document?

Not Dennis Byrne said...

C'mon guys, there's really only one other answer to the question :)

I'll hive you a hint ... not true . But you're gonna have to explain why to get the cool points on this one!

Anonymous said...

False - AtomicInteger is not an Integer, they are for use when updating integers concurrently. So it should be false, however

one.intValue().equals(two.intValue()) should return true.


Not Dennis Byrne said...

suresh, you are correct my friend.

AtomicInteger.equals uses instance equality because it is inherited from Object. I would assume this is by design.

The same for AtomicBoolean and AtomicLong as well.

name said...

Nice Article.