Java Absolute Value: Method & Examples

An error occurred trying to load this video.

Try refreshing the page, or contact customer support.

Coming up next: How to Use Pi Constant in Java

You're on a roll. Keep up the good work!

Take Quiz Watch Next Lesson
 Replay
Your next lesson will play in 10 seconds
  • 0:04 Java & Math Class
  • 0:44 Finding Absolute Value
  • 1:32 Infinity & Absolute Infinity
  • 1:50 Use For Abs
  • 2:22 Lesson Summary
Add to Add to Add to

Want to watch this again later?

Log in or sign up to add this lesson to a Custom Course.

Log in or Sign up

Timeline
Autoplay
Autoplay
Speed
Lesson Transcript
Instructor: Martin Gibbs

Martin has 16 years experience in Human Resources Information Systems and has a PhD in Information Technology Management. He is an adjunct professor of computer science and computer programming.

Java provides a wealth of math functions. This lesson will cover the Java method for determining the absolute value of a number. Working code examples are provided, covering the data types for which absolute value can be found.

Java & Math Class

Java comes with an entire library of math and arithmetic methods. Most of these are packaged within a core class, the Math class. The good news is that this class comes ready-to-use, right out of the box. Therefore, you can start using some powerful math functions in your code. One of these methods can be used to determine the absolute value.

The absolute value of a number is the positive representation of that number. For example, the absolute value of -3 is 3. It doesn't mean that the number is rounded or truncated (as we'll see in the code examples). Technically, absolute indicates the distance from the origin (0) to the number; which is why the absolute value of -3 is 3.

Finding Absolute Value

In order to retrieve the absolute value, we use the Java abs method. This method is in the Math class. The following code snippet shows how you access the method from the class:

Math.abs(variable);

The variable can be any primitive type; that is byte, short, int, long, float, or double. The following code shows each of these types, mixing in some negative, positive, and floating-point numbers. Notice that the Math.abs(variable) code is the same for each statement.

public Abs {
  byte byteV = 3;
  short iou = -5;
  int uome = 5;
  long pages = -344378;
  float pay = 15.3498f;
  double rate = -15.3498;
 
  public void printValues()
   System.out.println("Byte = " + Math.abs(byteV));
   System.out.println("Short = " + Math.abs(iou));
   System.out.println("Int = " + Math.abs(uome));
   System.out.println("Long = " + Math.abs(pages));
   System.out.println("Float = " + Math.abs(pay));
   System.out.println("Double = " + Math.abs(rate));
  }
}

When we compile and run this code, the output below is displayed. Note that the abs method is only showing the absolute value, NOT a rounded or truncated value. Absolute value is only the distance from zero to the number.


Java abs method output


Also note that a few of the numbers have the same absolute value; this helps us highlight the purpose of the method.

To unlock this lesson you must be a Study.com Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use Study.com

Become a Study.com member and start learning now.
Become a Member  Back
What teachers are saying about Study.com
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 200 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? Study.com has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create An Account
Support