File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Qn on abs () method Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Qn on abs () method" Watch "Qn on abs () method" New topic
Author

Qn on abs () method

cs singh
Ranch Hand

Joined: Dec 28, 2005
Posts: 36

why the ans is c here.?
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2341
The reason is because of the asymetry in the number of negative and positive integers that can be represented.

There are 2^31 negative whole numbers representable by an int, and 2^31-1 positive whole numbers representable by an int.

So there is one more negative whole number representable than there are positive whole numbers representable.

So Integer.MIN_VALUE has no positive counterpart.

Another way to look at it is the bit representation.

This is the bit representation of Integer.MIN_VALUE

10000000000000000000000000000000

In order to get the negative of a number we apply the complement operator to get the 1's complement and add 1 to get the 2's complement.

When we apply the complement operator we get

01111111111111111111111111111111

When we add 1 we get

10000000000000000000000000000000
[ March 14, 2006: Message edited by: Keith Lynn ]
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8712
And for those of you studying for the 1.5 exam, don't panic! This topic isn't on the exam.


Eliminate fossil fuel subsidies. (If you're not on the edge, you're taking up too much room.)
cs singh
Ranch Hand

Joined: Dec 28, 2005
Posts: 36
Thanks a lot Keith for your valuable time...
got it now.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Qn on abs () method
 
Similar Threads
Wrapper class
abs()'s crazy behaviour
abs() method
interesting
Math.abs(Integer.MIN_VALUE)