BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Get absolute value of float, int, double and long using Math.abs in Java

Last Updated: September 11, 2022 by Chaitanya Singh | Filed Under: java

Description

In this tutorial we are gonna see a program to find out the absolute values of float, int, double and long variables in java.
public static double abs(double a): Returns the absolute value of a double value.
public static float abs(float a): Returns the absolute value of a float value.
public static long abs(long a): Returns the absolute value of a long value.
public static int abs(int a): Returns the absolute value of a int value.
Note: The below points are applicable for all the above four variations of abs() method
If the argument is not negative, the argument is returned. If the argument is negative, the negation of the argument is returned. Special cases:
If the argument is positive zero or negative zero, the result is positive zero.
If the argument is infinite, the result is positive infinity.
If the argument is NaN, the result is NaN.
In other words, the result is the same as the value of the expression:
Double.longBitsToDouble((Double.doubleToLongBits(a)<<1)>>>1). Source.

Example

class AbsOfValues {

   public static void main(String[] args) {

      // variables of type double
      double dvar = 123456.99d;
      double dvar2 = -445.889d;

      // Displaying Absolute values
      System.out.println("Math.abs(" +dvar+ "): " + Math.abs(dvar));
      System.out.println("Math.abs(" +dvar2+ "): " + Math.abs(dvar2));
	      
      // variables of type float
      float fvar = 1256.99f;
      float fvar2 = -45.889f;

      // Displaying Absolute values
      System.out.println("Math.abs(" +fvar+ "): " + Math.abs(fvar));
      System.out.println("Math.abs(" +fvar2+ "): " + Math.abs(fvar2));
	      
      // variables of type long
      long lvar = 12345999L;
      long lvar2 = -475889L;

      // Displaying Absolute values
      System.out.println("Math.abs(" +lvar+ "): " + Math.abs(lvar));
      System.out.println("Math.abs(" +lvar2+ "): " + Math.abs(lvar2));
      
      // variables of type int
      int ivar = 1234;
      int ivar2 = -44;

      // Displaying Absolute values
      System.out.println("Math.abs(" +ivar+ "): " + Math.abs(ivar));
      System.out.println("Math.abs(" +ivar2+ "): " + Math.abs(ivar2));
  }
}

Output:

Math.abs(123456.99): 123456.99
Math.abs(-445.889): 445.889
Math.abs(1256.99): 1256.99
Math.abs(-45.889): 45.889
Math.abs(12345999): 12345999
Math.abs(-475889): 475889
Math.abs(1234): 1234
Math.abs(-44): 44

Top Related Articles:

  1. Java StringBuffer setLength() Method
  2. Java Math Class
  3. Java Math.random() Method
  4. Java Math.exp() Method
  5. Java Math.max() Method

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

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

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap