Topic -> Lang -> Math Q 14: Question 14 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 answer: a c e f sorry, i don't think F is correct. say Math.abs(Interger.MIN_VALUE) == Integer.MIN_VALUE.
SCJP 1.4 100% @ Peking, China <br />~~~~~~~~~~~~~~~~~~~~~<br />但使龙城飞将在, 不教胡马度阴山!
Joined: Nov 12, 2002
OH That is the latest version Dec. 4 Thx a lot for Your hard working for us.
f is correct. The Java API for Math.abs says: If the argument is negative, the negation of the argument is returned. this is exactly what Dan expresses in the option f. If arg is negative, that is, -2 for instance, then -(-2), that is, 2 is returned. Math.abs always returns positive values.
Of course API is not wrong at this point. The thing is the case you're presenting is very specific and is an exception. This happens because integers in Java are signed, so the MSB is for the sign of the number. Integer.MIN_VALUE = 0x8FFFFFFF
hi,Alfred, yes, i konw and i know why abs(Integer.MIN_VALUE) still equals itself. ,it's a exception. but, that's not question. the question which we are talk about is : is that expression RIGHT? because the EXCEPTION, reduction to absurdity , the expression is error. at least, it's not accurate . That's all