Java Programming - Java.lang Class - Discussion

Discussion Forum : Java.lang Class - General Questions (Q.No. 2)
2.
Which of the following would compile without error?
int a = Math.abs(-5);
int b = Math.abs(5.0);
int c = Math.abs(5.5F);
int d = Math.abs(5L);
Answer: Option
Explanation:

The return value of the Math.abs() method is always the same as the type of the parameter passed into that method.

In the case of A, an integer is passed in and so the result is also an integer which is fine for assignment to "int a".

The values used in B, C & D respectively are a double, a float and a long. The compiler will complain about a possible loss of precision if we try to assign the results to an "int".

Discussion:
3 comments Page 1 of 1.

Vishnu said:   9 years ago
The syntax of that method is : static double abs(double arg)

If we pass int value int will return and if we pass double value will return then how can I staisfy with this answer. Can you any one explain?

Vikas said:   1 decade ago
int a = Math.abs(-5) ;

-5 is a valid parameter of int.

Dev said:   1 decade ago
What is parameter passed into the method?

Post your comments here:

Your comments will be displayed after verification.