Integer.MIN_VALUE is -2147483648, but the highest value a 32 bit integer can contain is +2147483647. Attempting to represent +2147483648 in a 32 bit int will effectively "roll over" to -2147483648. This is because, when using signed integers, the two's complement binary representations of +2147483648 and -2147483648 are identical. This is not a problem, however, as +2147483648 is considered out of range.

For a little more reading on this matter, you might want to check out the Wikipedia article on Two's complement.

Answer from jonmorgan on Stack Overflow
🌐
W3Schools
w3schools.com › java › ref_math_abs.asp
Java Math abs() Method
The abs() method returns the absolute (positive) value of a number. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com · If you want to report an error, or if ...
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › Math.html
Math (Java Platform SE 8 )
October 20, 2025 - if the second argument is a finite ... the absolute value of the first argument to the power of the second argument · if the second argument is finite and not an integer, then the result is NaN. If both arguments are integers, then the result is exactly equal to the mathematical result of ...
Discussions

Which Java method is used to find the absolute value of a number? Question 9Select one: a. Math.abs() b. Math.sqrt()...
Which Java method is used to find the absolute value of a number? Question 9Select one: a. Math.abs() b. More on coursesidekick.com
🌐 coursesidekick.com
1
October 21, 2025
java - Math.abs returns wrong value for Integer.Min_VALUE - Stack Overflow
I apologise for my mistake here: I tought that you proposed the use of Math.abs(long) as a fix, when you showed it as a simple way to "see the result the asker is expecting". Sorry. 2013-06-22T23:19:35.13Z+00:00 ... In Java 15 with the new methods in fact a Exception is thrown. More on stackoverflow.com
🌐 stackoverflow.com
Should I be using Math.abs?
I'm pretty new to java myself, but doesn't Math.abs return the absolute value, so it returns negative numbers as positive? I'd have gone with else if (18 < age > 24) More on reddit.com
🌐 r/learnjava
4
1
July 31, 2015
import - java.lang.Math.abs not imported by default? - Stack Overflow
I'm studying for a beginners Java Exam through Oracle. One of the questions says given: int absoluteValue = abs(-21) What import statement will compile all the code? Correct answer given as: im... More on stackoverflow.com
🌐 stackoverflow.com
🌐
GitHub
gist.github.com › BrandonLMorris › 68a91fc1e066eb38f774
Pointing out an interesting quirk with Java's Math.abs() where it actually returns a negative number · GitHub
Pointing out an interesting quirk with Java's Math.abs() where it actually returns a negative number - ConvertToPositive.java
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-math-abs-method-examples
Java Math abs() method with Examples - GeeksforGeeks
July 11, 2025 - The java.lang.Math.abs() returns the absolute value of a given argument.
🌐
BeginnersBook
beginnersbook.com › 2022 › 10 › java-math-abs-method
Java Math.abs() Method
October 8, 2022 - Java Math.abs() method returns an absolute value of the given number.
🌐
CodeGym
codegym.cc › java blog › java math › java math abs() method
Java Math abs() method
January 9, 2025 - For example, the absolute of +5 is 5. Whereas, the absolute of -5 is also 5. The java.lang.Math class provides a static method Math.abs(parameter) to find the “absolute value” of the parameter.
Find elsewhere
🌐
AlphaCodingSkills
alphacodingskills.com › java › notes › java-math-abs-double.php
Java Math abs() Method - AlphaCodingSkills
The java.lang.Math.abs() method returns the absolute value of a double value. If the argument is not negative, the argument is returned. If the argument is negative, the negation of the argument is returned. In special cases it returns the following: If the argument is positive zero or negative ...
🌐
iO Flood
ioflood.com › blog › java-absolute-value
The Multiple Methods to Find Absolute Value in Java
February 29, 2024 - Are you struggling to find the absolute value in Java? You’re not alone. Many developers find themselves puzzled when it comes to handling absolute values in Java, but we’re here to help. Think of Java’s Math.abs() function as a compass – guiding you to the true north in the world of numbers.
Top answer
1 of 8
124

Integer.MIN_VALUE is -2147483648, but the highest value a 32 bit integer can contain is +2147483647. Attempting to represent +2147483648 in a 32 bit int will effectively "roll over" to -2147483648. This is because, when using signed integers, the two's complement binary representations of +2147483648 and -2147483648 are identical. This is not a problem, however, as +2147483648 is considered out of range.

For a little more reading on this matter, you might want to check out the Wikipedia article on Two's complement.

2 of 8
51

The behaviour you point out is indeed, counter-intuitive. However, this behaviour is the one specified by the javadoc for Math.abs(int):

If the argument is not negative, the argument is returned. If the argument is negative, the negation of the argument is returned.

That is, Math.abs(int) should behave like the following Java code:

public static int abs(int x){
    if (x >= 0) {
        return x;
    }
    return -x;
}

That is, in the negative case, -x.

According to the JLS section 15.15.4, the -x is equal to (~x)+1, where ~ is the bitwise complement operator.

To check whether this sounds right, let's take -1 as example.

The integer value -1 is can be noted as 0xFFFFFFFF in hexadecimal in Java (check this out with a println or any other method). Taking -(-1) thus gives:

-(-1) = (~(0xFFFFFFFF)) + 1 = 0x00000000 + 1 = 0x00000001 = 1

So, it works.

Let us try now with Integer.MIN_VALUE . Knowing that the lowest integer can be represented by 0x80000000, that is, the first bit set to 1 and the 31 remaining bits set to 0, we have:

-(Integer.MIN_VALUE) = (~(0x80000000)) + 1 = 0x7FFFFFFF + 1 
                     = 0x80000000 = Integer.MIN_VALUE

And this is why Math.abs(Integer.MIN_VALUE) returns Integer.MIN_VALUE. Also note that 0x7FFFFFFF is Integer.MAX_VALUE.

That said, how can we avoid problems due to this counter-intuitive return value in the future?

  • We could, as pointed out by @Bombe, cast our ints to long before. We, however, must either

    • cast them back into ints, which does not work because Integer.MIN_VALUE == (int) Math.abs((long)Integer.MIN_VALUE).
    • Or continue with longs somehow hoping that we'll never call Math.abs(long) with a value equal to Long.MIN_VALUE, since we also have Math.abs(Long.MIN_VALUE) == Long.MIN_VALUE.
  • We can use BigIntegers everywhere, because BigInteger.abs() does indeed always return a positive value. This is a good alternative, though a bit slower than manipulating raw integer types.

  • We can write our own wrapper for Math.abs(int), like this:

/**
 * Fail-fast wrapper for {@link Math#abs(int)}
 * @param x
 * @return the absolute value of x
 * @throws ArithmeticException when a negative value would have been returned by {@link Math#abs(int)}
 */
public static int abs(int x) throws ArithmeticException {
    if (x == Integer.MIN_VALUE) {
        // fail instead of returning Integer.MAX_VALUE
        // to prevent the occurrence of incorrect results in later computations
        throw new ArithmeticException("Math.abs(Integer.MIN_VALUE)");
    }
    return Math.abs(x);
}
  • Use a integer bitwise AND to clear the high bit, ensuring that the result is non-negative: int positive = value & Integer.MAX_VALUE (essentially overflowing from Integer.MAX_VALUE to 0 instead of Integer.MIN_VALUE)

As a final note, this problem seems to be known for some time. See for example this entry about the corresponding findbugs rule.

🌐
Reddit
reddit.com › r/learnjava › should i be using math.abs?
r/learnjava on Reddit: Should I be using Math.abs?
July 31, 2015 -

I'm trying to test to see if a number is between 18-24 Im working through programmingbydoing.com Lesson 25.

import java.util.Scanner;

public class HowOldAreYou2 { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); String name; int age;

    System.out.print("What your name?");
    name = keyboard.next();

    System.out.print(name + ", how old are you?");
    age = keyboard.nextInt();

    if (age < 16)
    {
        System.out.print("You cant drive");
    }
    else if (age == 16 | age == 17)
    {
        System.out.print("You can drive but you can't vote");
    }
    else if (age == Math.abs(18-24))
    {
        System.out.print("You can vote but not rent a car");
    }

    
}

}

Top answer
1 of 3
2
I'm pretty new to java myself, but doesn't Math.abs return the absolute value, so it returns negative numbers as positive? I'd have gone with else if (18 < age > 24)
2 of 3
2
age == Math.abs(18-24) will only result in true if age is 6. It returns the positive version of its argument, the argument being 18 minus 24. The cleanest way to write your if structure would be this: if (age <= 15) { } else if (age <= 17) { } else if (age <= 24) { } because it 1) actually works, 2) uses the same kind of expression in all cases, so it's nicely readable, and 3) it expresses intention: It says that you intend to cover the range of ages without gaps. ... I'll now read carefully in your post whether that's actually what you want - see what I'm saying? But you could also use a switch structure. It might not be a good way to do it here, but I want to show you this alternative, plus it invites you to add more age-related text output. At the same time, it gives you an opportunity to be aware of the effect that "feature creep" is, albeit in a minuscule case. It's important to develop an awareness for this effect. It's one of the top project killers. switch(age) { case 1: case 2: case 3: System.out.println("Do your parents know that you're using the computer?"); break; case 4: case 5: case 6: case 7: case 8: case 9: System.out.println("Look at you - getting ready for the IT world, are we."); break; case 10: case 11: case 12: case 13: case 14: case 15: System.out.print("You're too young to drive."); break; case 16: case 17: System.out.print("You can drive, but you can't vote."); break; case 18: case 19: case 20: case 21: case 22: case 23: case 24: System.out.print("You can vote but not rent a car."); break; default: System.out.print("You're too old to be considered relevant for this program."); } Will they ever fix the line numbering style sheets?
🌐
Turing
turing.com › kb › java-absolute-value
Java Math Absolute Value Abs() Method
Similarly, this class has a static method called calculateAbsoluteValue() that takes a double number as input and uses the Math.abs() function to calculate the absolute value. The method returns the absolute value as a double.
🌐
Edureka
edureka.co › blog › java-math-abs
Java Math Abs Method | Absolute Method In Java | Edureka
September 13, 2019 - public class Example2 { public static void main(String args[]) { double x = -27.64; double y = -394.27; //printing absolute value of double type System.out.println(Math.abs(x)); System.out.println(Math.abs(y)); System.out.println(Math.abs(7.0 / 0)); //infinity } } Output – 27.64 394.27 Infinity Moving on with this article on Java Math abs() float-
🌐
Dot Net Perls
dotnetperls.com › math-abs-java
Java - Math.abs: Absolute Value - Dot Net Perls
With logical tests, we can check for negative and positive numbers. But Math.abs streamlines this. It makes some programs simpler and easier to reason about. To start, an absolute value is the number with no negative sign. If the number is already positive, no change is made. We first import java....
Top answer
1 of 3
4

But my question is if java.lang.* is imported by default then why is the Math class not imported and the abs method not available?

Because it isn't.

Because that is the way that Java works. An implicit (or explicit) wildcard import of the classes in a package only imports the classes. It doesn't also do a static import of class members.

If you want to refer to all static members of a class without qualifying them, you should use a wildcard static import; e.g.

import static java.lang.Math.*;

Alternatively you can static import individual members; e.g.

import static java.lang.Math.abs;

Why did they define Java this way?

Well it is most likely rationale is that implicit importing makes it harder to read code. If methods like abs get imported by default, then you need to know what they all are ... and where they are imported from ... in order to understand the true meaning of the code.

It is worth knowing that static imports were only added in Java 5. Prior to that, it was not possible to refer to Math.abs without the Math qualification.


If you just import the class not its static members then what are you getting when you import it?

You just get the class name. For example:

import java.util.HashMap;

allows me to write new HashMap() rather than new java.util.HashMap() etcetera. This is significant. (Imagine if you always had to refer to classes by their full name ....)

2 of 3
1

you have to call abs() method on class name of math class Math.abs(), It is static method.

or you have to import import static java.lang.Math.abs;

Internal Implementation of Math class absolute() method.

public static long  abs(long a) {
    return (a < 0) ? -a : a;
 }

abs() method is static method, java.lang.*; can not import static member of class.

🌐
Programmers
programmers.io › home › blog › absolute value abs () in java
Absolute Value Abs () In Java - Programmers.io
June 26, 2025 - The Math.abs () method is used in Java development to obtain the absolute value of a number. It belongs to the java.lang.Math class, which is a part of the core Java API.
🌐
Verve AI
vervecopilot.com › interview-questions › why-does-understanding-absolute-value-java-sharpen-your-interview-edge
Why Does Understanding Absolute Value Java Sharpen Your Interview Edge?
September 5, 2025 - Efficiency: Knowing and using built-in methods like Math.abs() is more efficient and less error-prone than manually writing conditional logic. Attention to Detail: Recognizing when to use absolute value often hints at an interviewer that you think about all possible input states, including negative numbers. For coding interviews, understanding absolute value Java demonstrates several key traits:
🌐
SAP Community
community.sap.com › t5 › technology-q-a › problem-with-abs-arithmetic-function › qaq-p › 999905
Solved: Problem with ABS Arithmetic Function - SAP Community
August 12, 2005 - I have screen shots of the problem scenario and i have also built a test case just to test the ABS function. This may be helpful when SAP wants to dial in and have a look. Let me know if the screen shots will help you. I can forward to your email ID. ... I'm giving you full points.... because we got the same reply from SAP ... ...Floating point calculations are not precise - this is point is well-described in Java specification. As a workaround you can create a set of own arithmetic functions which use class java.math.BigDecimal for calculations.
🌐
Tutorialspoint
tutorialspoint.com › home › java/lang › java math.abs() method
Java Math.abs() Method
September 1, 2008 - The Java Math abs(double a) returns the absolute value of a double value. If the argument is not negative, the argument is returned.
🌐
TutorialKart
tutorialkart.com › java › java-math › java-math-abs
Java Math abs() - Absolute Value
November 23, 2020 - Math toRadians · Advanced · Java Date & Time · Java MySQL · abs() accepts an argument and returns its absolute value. Datatype of the argument could be double, float, int or long and the return type would be same as that of argument.