Java Math cbrt() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Java 5+

Definition

In Java, Math.cbrt(a) returns the cube root of a: the number that gives a when multiplied by itself three times. Math.cbrt(27) is 3.0.

Use it to go from a volume to an edge length, or to undo a cubed value. It was added in Java 5 (JDK 1.5), and it accepts negative numbers: Math.cbrt(-8) is -2.0.

A static method of the java.lang.Math class, added in Java 5 (JDK 1.5). Call it on the class name, as in Math.cbrt(...). No import is needed, because classes in java.lang are always visible.

Syntax

Pass any number; it is widened to double, and the result is a double.

Java
double a = Math.cbrt(27);          // 3.0
double b = Math.cbrt(-8);          // -2.0
double edge = Math.cbrt(volume);   // side of a cube
long r = Math.round(Math.cbrt(n)); // nearest whole root
CallWhat it does
Math.cbrt(125)Returns about 5.0.
Math.cbrt(-27)Negative input: returns about -3.0.
Math.cbrt(volume)Edge length of a cube with that volume.
Math.round(Math.cbrt(n))Nearest whole cube root, for perfect-cube checks.

Java 5 or newer is needed. The result is allowed to be off by one unit in the last place, so round it (or compare with a tolerance) before testing for whole numbers.

Library declarations
Java
// java.lang.Math (Java 5+)
public static double cbrt(double a)

Parameters

ParameterDescription
aThe value whose cube root you want. It may be negative, zero, or positive.

Return Value

Return ValueDescription
doubleThe cube root of a. For negative a the result is negative. NaN gives NaN, infinities stay infinite with the same sign, and zeros keep their sign.

Compare with Similar Methods

Other ways to get roots:

MethodHow it differs
pow(a, 1.0 / 3)Gives NaN for negative numbers and can be slightly off.
sqrt(a)The square root; negative input gives NaN.
round(x)Turns a cube root into the nearest whole number.
exp(log(a) / 3)A manual route that fails for negative a; not needed with cbrt.

Java Examples

Example 1 — Cube Roots and the Edge of a Cube

Take cube roots of exact cubes, then find the edge of a cube-shaped tank that holds 100 liters.

Java
public class MathCbrtBasics {
    public static void main(String[] args) {
        System.out.println("cbrt(27) = " + Math.cbrt(27));
        System.out.println("cbrt(-8) = " + Math.cbrt(-8));

        double volume = 100;
        double edge = Math.cbrt(volume);
        System.out.println("Edge for 100 liters: " + Math.round(edge * 100) / 100.0);
        System.out.println("Volume check: " + Math.round(edge * edge * edge));
    }
}

How It Works

  1. Exact cubes. 3 * 3 * 3 is 27 and (-2) * (-2) * (-2) is -8, and cbrt returns those roots as 3.0 and -2.0, the negative one carrying its minus sign.
  2. Not a perfect cube. 100 lies between 4 cubed (64) and 5 cubed (125), so the edge is about 4.6416; rounding to two decimals shows 4.64.
  3. Cubing back. Multiplying the edge by itself three times returns to about 100.0 (maybe a hair off); Math.round gives the clean long 100.

Example 2 — Why cbrt() Beats pow() for Negative Numbers

Compare the two ways of taking a cube root, then see how the special values behave.

Java
public class MathCbrtNegatives {
    public static void main(String[] args) {
        System.out.println("cbrt(-8):         " + Math.cbrt(-8));
        System.out.println("pow(-8, 1.0 / 3): " + Math.pow(-8, 1.0 / 3));
        System.out.println("cbrt(-0.0):       " + Math.cbrt(-0.0));
        System.out.println("cbrt(-Infinity):  " + Math.cbrt(Double.NEGATIVE_INFINITY));
        System.out.println("cbrt(NaN):        " + Math.cbrt(Double.NaN));
    }
}

How It Works

  1. Odd roots exist below zero. A negative number cubed stays negative, so every negative value has a real cube root, and cbrt finds it.
  2. pow gives up. The exponent 1.0 / 3 is not a whole number, and pow refuses negative bases in that case, returning NaN.
  3. Signs are preserved. Negative zero and negative infinity come back with their minus signs, while NaN in means NaN out.

Example 3 — Check Which Numbers Are Perfect Cubes

Round the cube root to the nearest whole number, cube it again, and compare with the original value.

Java
public class MathCbrtPerfectCube {
    public static void main(String[] args) {
        long[] values = {27, 28, -64, 1000};
        for (long n : values) {
            long r = Math.round(Math.cbrt(n));
            System.out.println(n + ": nearest root " + r + ", cube? " + (r * r * r == n));
        }
    }
}

How It Works

  1. Round, do not cast. Math.round picks the nearest whole root even if cbrt returns something like 2.9999999999999996, where a plain (long) cast would give 2.
  2. Cube it back with integers. r * r * r is exact long arithmetic, so equality with n is a real test: 28 fails because 3 cubed is 27.
  3. Negatives work. cbrt(-64) is about -4.0, the rounded root is -4, and (-4) cubed is -64, so the test passes without any special case for the sign.

Frequently Asked Questions

It returns the cube root of a double. Math.cbrt(27) returns 3.0 and Math.cbrt(-8) returns -2.0.
Java 5 (JDK 1.5). Older versions need Math.pow(x, 1.0 / 3), which fails for negative numbers.
pow does not return a real result for a negative base with a non-whole exponent, and 1.0 / 3 is not whole. cbrt is built for cube roots, so it accepts negative input.
Usually the result is the exact whole number, but Java only promises it is within one unit in the last place. Round with Math.round before comparing to a whole number.
Call Math.cbrt(volume). For a volume of 100 the side is about 4.64.

Next: ceil()

Round a double up to the next whole number (the result is still a double).

ceil() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

14 people found this page helpful