Win a copy of Kotlin in Action this week in the Kotlin forum!
programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering Languages Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

Andres Gonzalez
Ranch Hand
Posts: 1561
Which of the following statements are true in terms of the java.lang.Math.abs method?
a. Four overloaded versions of abs exist.
b. An ArithmeticException is declared in the throws clause.
c. The type of the return value depends on the type of the argument.
d. The returned value is always of a floating-point primitive type.
e. If the argument is greater than or equal to zero then the returned value is equal to the argument.
f. If the argument, arg, is less than zero then the returned value is -arg.
g. None of the Above
Corrects : a, c, e,f
why option f? I'd say:
f. If the argument, arg, is less than zero then the returned value is arg.

Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
If the argument is less than zero then the returned value is -arg...
Let's say that the arg is -15 (a number less than zero). Then the returned value will be +15. But +15 is -(-15). If abs returned arg then it would return -15 which isn't what abs does. It only returns positive numbers.

Marlene Miller
Ranch Hand
Posts: 1392
Math.abs(Integer.MIN_VALUE) == -Integer.MIN_VALUE
-Integer.MIN_VALUE == Integer.MIN_VALUE
Integer.MIN_VALUE < 0
[ June 13, 2003: Message edited by: Marlene Miller ]

chi Lin
Ranch Hand
Posts: 348
In general, option (f) is true ... ,try the code

output :
Integer.MIN_VALUE = -2147483648
Math.abs(Integer.MIN_VALUE) = -2147483648
I believe Dan's mock exam has something similar.
[ June 13, 2003: Message edited by: chi Lin ]

Marlene Miller
Ranch Hand
Posts: 1392
In math, the absolute value of a real number is always >= 0.
In Java, there is one int and one long whose absolute value < 0.
[ June 13, 2003: Message edited by: Marlene Miller ]

Sudhakar Krishnamurthy
Ranch Hand
Posts: 76
In math, the absolute value of a real number is always >= 0.
In Java, there is one int and one long whose absolute value < 0.

What could be the rational behind this just providing one int/long whose abs value is < 0...any thoughts??
TIA

Alton Hernandez
Ranch Hand
Posts: 443
Originally posted by Andres Gonzalez:
why option f? I'd say:
f. If the argument, arg, is less than zero then the returned value is arg.

I think the Math.abs is doing exactly of what is expected of it - with a few exception.
From Math.abs API doc:

If the argument is not negative, the argument is returned. If the argument is negative, the negation of the argument is returned.

I think the confussion stems from the rewording of the phrase "negation of the argument" to "-arg". (f) could be interpreted as abs returning a negative value if you pass it a negative value.

Marlene Miller
Ranch Hand
Posts: 1392
What could be the rational behind this just providing one int/long whose abs value is < 0...any thoughts??

I don�t know. What else could Math.abs(Integer.MIN_VALUE) be? an ArithmeticException. Interger.MAX_VALUE + 1 < 0. The addition does not cause an exception. We live with it.
The Java Programming Language (where I get my insights into language design) says "the absolute value of the most negative value of an int or long its itself and therefore negative; that's how two's complement integers work.
Here is the source code.
public static int abs(int a) { return (a < 0) ? -a : a; }
[ June 14, 2003: Message edited by: Marlene Miller ]