How do you get a square root and an absolute value in Java?
Here is what I have:
if (variable < 0) {
variable = variable + variable2;
}
But is there an easier way to get the absolute value in Java?
variable = |variable|
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Use the static methods in the
Mathclass for both – there are no operators for this in the language:(Likewise there’s no operator for raising a value to a particular power – use
Math.powfor that.)If you use these a lot, you might want to use static imports to make your code more readable:
instead of