Home > Tutorial > Java Absolute Value Sample Code

Java Absolute Value Sample Code

The Java Match Class comes with four different methods that all return the absolute value of a number. The only difference between the methods is the data type of the argument and the data type of the return type.

The table below shows the four Math.abs() methods as well as the input and return, which can be of type int, long, float, and double.

Method Argument Return Type
abs() int int
abs() double double
abs() float float
abs() long long


Java Absolute Value Integer

This example will use the Integer version of the abs() method. The method will take an Integer and return an Integer that represents the Absolute Value.

int jcdInteger = 950;
System.out.println("\nAbsolute Value (int): "+Math.abs(jcdInteger));
Absolute Value (int): 950


Java Absolute Value Double

This example will use the Double version of the abs() method. The method will take an Double and return a Double that represents the Absolute Value.

double jcdDouble = 522.50;
System.out.println("\nAbsolute Value (double): "+Math.abs(jcdDouble));
Absolute Value (double): 522.5


Java Absolute Value Float

This example will use the Float version of the abs() method. The method will take an Float and return the Absolute Value as a Float variable.

float jcdFloat = 3.568921F;
System.out.println("\nAbsolute Value (float): "+Math.abs(jcdFloat));
Absolute Value (float): 3.568921


Java Absolute Value Long

This example will use the Long version of the abs() method. The method will accept a Long and return a Long for the Absolute Value.

long jcdLong = 345678901L;
System.out.println("\nAbsolute Value (long): "+Math.abs(jcdLong));
Absolute Value (long): 345678901


Java Absolute Value Source Code

The samplesource code below is the full class used to create this Java tutorial on Math.abs() method.

package com.jcd.java.tutorials;

public class JavaAbsoluteValue
{

	public static void main(String[] args) 
	{
		
		//Absolute Value Integer
		int jcdInteger1 = 950;
		int jcdInteger2 = -820;
		
		System.out.println("\nAbsolute Value (int): "+Math.abs(jcdInteger1));
		System.out.println("Absolute Value (int): "+Math.abs(jcdInteger2));
		
		//Absolute Value Double
		double jcdDouble1 = 522.50;
		double jcdDouble2 = -612.80;
		
		System.out.println("\nAbsolute Value (double): "+Math.abs(jcdDouble1));
		System.out.println("Absolute Value (double): "+Math.abs(jcdDouble2));
		
		//Absolute Value Float
		float jcdFloat1 = 3.568921F;
		float jcdFloat2 = -2.56734F;
		
		System.out.println("\nAbsolute Value (float): "+Math.abs(jcdFloat1));
		System.out.println("Absolute Value (float): "+Math.abs(jcdFloat2));
		
		//Absolute Value Float
		long jcdLong1 = 345678901L;
		long jcdLong2 = -23456781L;
		
		System.out.println("\nAbsolute Value (long): "+Math.abs(jcdLong1));
		System.out.println("Absolute Value (long): "+Math.abs(jcdLong2));

	}

}
Absolute Value (int): 950
Absolute Value (int): 820

Absolute Value (double): 522.5
Absolute Value (double): 612.8

Absolute Value (float): 3.568921
Absolute Value (float): 2.56734

Absolute Value (long): 345678901
Absolute Value (long): 23456781



Java References:

Smedley
Posted by

Smedley

Smedley has been programming in Java for about twenty years. He enjoys sharing code samples to other fellow Java coders.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

Copyright © 2018 JavaCodeDepot