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) );

7 comments:

akash said...

True

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 http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Integer.html#equals(java.lang.Object)

And I guess, we should already read the API document? http://java.sun.com/j2se/1.4.2/docs/api/java/math/BigDecimal.html#BigDecimal(double)

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.

-suresh

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.