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 ....)

Answer from Stephen C on Stack Overflow
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.

๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ java-math-abs-method-examples
Java Math abs() method with Examples - GeeksforGeeks
July 11, 2025 - // Java Program to Demonstrate Working of abs() method // of Math class inside java.lang package // Importing Math class // from java.lang package import java.lang.Math; // Main class class GFG { // Main driver method public static void main(String args[]) { // Customly declaring and initializing all // arguments that ans() function takes // Float float a = 123.0f; float b = -34.2323f; // Double double c = -0.0; double d = -999.3456; // Integer int e = -123; int f = -0; // Long long g = -12345678; long h = 98765433; // abs() method taking float type as input System.out.println(Math.abs(a)); Sy
๐ŸŒ
Programiz
programiz.com โ€บ java-programming โ€บ library โ€บ math โ€บ abs
Java Math abs()
class Main { public static void main(String[] args) { // create variables int a = 7; long b = -23333343; double c = 9.6777777; float d = -9.9f; // print the absolute value System.out.println(Math.abs(a)); // 7 System.out.println(Math.abs(c)); // 9.6777777 // print the value without negative sign System.out.println(Math.abs(b)); // 23333343 System.out.println(Math.abs(d)); // 9.9 } } In the above example, we have imported the java.lang.Math package.
๐ŸŒ
W3Schools
w3schools.com โ€บ java โ€บ ref_math_abs.asp
Java Math abs() Method
assert abstract boolean break byte case catch char class continue default do double else enum exports extends final finally float for if implements import instanceof int interface long module native new package private protected public return requires short static super switch synchronized this throw throws transient try var void volatile while Java String Methods
๐ŸŒ
Turing
turing.com โ€บ kb โ€บ java-absolute-value
Java Math Absolute Value Abs() Method
By importing the Math class and calling the abs() function, we can calculate the absolute value of various integer data types, including integers, floating-point numbers, and long integers.
๐ŸŒ
Javatpoint
javatpoint.com โ€บ java-math-abs-method
Java Math.abs() method with Examples - Javatpoint
Java Math.abs() method with Examples on abs(), min(), max(), avg(), round(), ceil(), floor(), pow(), sqrt(), sin(), cos(), tan(), exp() etc.
๐ŸŒ
Bbminfo
bbminfo.com โ€บ java โ€บ library โ€บ math โ€บ abs.php
Java abs() - Java Math Functions - bbminfo
Convert the positive and negative float to an absolute value. import java.lang.Math; class MathExample { public static void main(String[] args) { // Positive floating point value double n = Math.abs(9.3); System.out.println("The absolute value ...
Find elsewhere
๐ŸŒ
Educative
educative.io โ€บ answers โ€บ what-is-the-mathabs-function-in-java
What is the Math.abs function in Java?
Math.abs(a); a: The argument from which the absolute value will be returned. If the value of the passed argument is: Negative, then the negative sign is removed and a positive value is returned.
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ home โ€บ java/lang โ€บ java math abs() method
Java Math abs() Method
September 1, 2008 - The Java Math abs(int a) returns the absolute value of an int value. If the argument is not negative, the argument is returned. If the argument is negative, the negation of the argument is returned.
๐ŸŒ
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 odd integer, the result is equal to the negative of the result of raising 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 raising the first argument to the power of the second argument if that result can in fact be represented exactly as a double value.
๐ŸŒ
CodeGym
codegym.cc โ€บ java blog โ€บ java math โ€บ java math abs() method
Java Math abs() method
January 9, 2025 - ... In Java, the Math class is part of the java.lang package, which is automatically imported. Therefore, you can use the Math.abs() function directly without any explicit import statement.
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ home โ€บ java โ€บ java number abs method
Java Number abs Method
September 1, 2008 - Java Vs. C++ ... The method gives the absolute value of the argument. The argument can be int, float, long, double, short, byte. ... Any primitive data type. This method Returns the absolute value of the argument. public class Test { public static void main(String args[]) { Integer a = -8; double d = -100; float f = -90; System.out.println(Math.abs(a)); System.out.println(Math.abs(d)); System.out.println(Math.abs(f)); } }
๐ŸŒ
AlphaCodingSkills
alphacodingskills.com โ€บ java โ€บ notes โ€บ java-math-abs-double.php
Java Math abs() Method - AlphaCodingSkills
In the example below, abs() method returns the absolute value of the given double values. import java.lang.*; public class MyClass { public static void main(String[] args) { double x = 10.55; double y = -10.55; System.out.println(Math.abs(x)); System.out.println(Math.abs(y)); } }
๐ŸŒ
Java Tutorial HQ
javatutorialhq.com โ€บ java tutorial โ€บ java.lang โ€บ math โ€บ abs() method example
Java Math abs() method example
September 30, 2019 - Below is a java code demonstrates the use of abs() method of Math class. The example presented might be simple however it shows the behavior of the abs() method. package com.javatutorialhq.java.examples; import static java.lang.System.out; /* * A java example source code to demonstrate * the use of abs() method of Math class */ public class MathAbsExample { public static void main(String[] args) { // initialize values int intVal = -123; float floatVal = 1.54f; double doubleVal = -3.27; // get the absoluteValue int intValAbs = Math.abs(intVal); float floatValAbs = Math.abs(floatVal); double doubleValAbs = Math.abs(doubleVal); out.println("Absolute value of int value is:"+intValAbs); out.println("Absolute value of float value is:"+floatValAbs); out.println("Absolute value of double value is:"+doubleValAbs); } }
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ abs() in java
abs() in Java - Scaler Topics
May 5, 2024 - The Math.abs() returns an absolute value of the number that is a non-negative representation of the number. The return value type depends upon the parameter; if the parameter's datatype is int or float, the return type will be int and float, ...
๐ŸŒ
Programmers
programmers.io โ€บ home โ€บ blog โ€บ absolute value abs () in java
Absolute Value Abs () In Java - Programmers.io
June 26, 2025 - This article provides the details of the Java Math.abs () method, including its usage, examples, and practical applications.
๐ŸŒ
Upgrad
upgrad.com โ€บ home โ€บ tutorials โ€บ software & tech โ€บ implementing and manipulating abs in java
Java Math abs() Method with Examples
March 6, 2025 - Bitwise operations can be useful for performance optimization in low-level systems programming, but in Java, they are generally unnecessary. The Math.abs() method is already optimized, making it the most efficient approach for handling absolute values in typical Java applications.
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ java โ€บ lang โ€บ math_abs_long.htm
Java - Math abs(long) Method
The Java Math abs(long a) returns the absolute value of a long value. If the argument is not negative, the argument is returned. If the argument is negative, the negation of the argument is returned.
๐ŸŒ
How to do in Java
howtodoinjava.com โ€บ home โ€บ java basics โ€บ java math.abs() and math.absexact() with examples
Java Math.abs() and Math.absExact() with Examples
January 14, 2024 - The absolute values are commonly used for computing/expressing distances, for example, distance from the sea level. The distance from sea level is always a positive number whether a scuba diver is diving under the sea or a mountain climber climbing on the mountain. The Math.abs() is used for ...