aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes abs()  method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "abs()  method" Watch "abs()  method" New topic
Author

abs() method

Sakthi Kani
Ranch Hand

Joined: Mar 29, 2005
Posts: 98
Actually,

abs() method returns a non-negative value of the argument.
--------------------------------------------------
System.out.println(Math.abs(Byte.MIN_VALUE));
System.out.println(Math.abs(Short.MIN_VALUE));
System.out.println(Math.abs(Float.MIN_VALUE));
System.out.println(Math.abs(Double.MIN_VALUE));
----------------------------------------------------

These four lines are returning positive value.

----------------------------------------------------
System.out.println(Math.abs(Integer.MIN_VALUE));
System.out.println(Math.abs(Long.MIN_VALUE));
----------------------------------------------------
But in this two cases it is returning negative value.why???

Can anyone clear my doubt....


" Don't be afraid of pressure. Remember that pressure is what turns a lump of coal into a diamond... " <br /> <br />Thanks & Regards...<br />Sakthi<br />SCJP1.4, OCA
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
Because of overflow.
Here is another apparant anomaly that is in fact not:
http://qa.jtiger.org/GetQAndA.action?qids=15&showAnswers=true


Tony Morris
Java Q&A (FAQ, Trivia)
Parameswaran Thangavel
Ranch Hand

Joined: Mar 01, 2005
Posts: 485
hi
i gone through the link but i cant get it.
can u please explain me with some examples
Raghu Shree
Ranch Hand

Joined: Mar 18, 2005
Posts: 143
Hi
The abs() method has following forms

int abs(int);
long abs(long);
float abs(float); and

Byte.MIN_VALUE return -128
Integer.MIN_VALUE returns -2147483648
Long.MIN_VALUE returns -9223372036854775808

abs(-128) overload abs(int) method so It doesn't return negative value/
abs(-2147483648) overload abs(int) method and return the value 2147483648. But this value greater than integer range. so It is return negative value.
abs( -9223372036854775808) overlaod abs(long) method and return the value -9223372036854775808. But this is larger than long range. So It also returns negative value.


Raghu J<br />SCJP 1.4<br /> <br />The Wind and waters are always<br />on the side of the ablest navigators.<br /><a href="http://groups.yahoo.com/group/scjp_share" target="_blank" rel="nofollow">SCJP Group</a><br /><a href="http://groups.yahoo.com/group/JavaBeat_SCWCD" target="_blank" rel="nofollow">SCWCD Group</a>
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: abs() method