Java Math.cbrt
In Java, Math.cbrt returns the real cube root. The cube root of −8 is −2 because (−2) × (−2) × (−2) = −8.
A simple example
Math.cbrt
- Start with 8
- Do this Find the cube root
- Result 2
class Main {
public static void main(String[] args) throws Exception {
System.out.println(Math.cbrt(8.0));
}
}
2.0
Syntax
Math.cbrt(volume)
JDK 21 method signatures (1)
public static double cbrt(double a)Run these examples in Java
Save one example at a time as Main.java. Use JDK 21.
java Main.javaMeasure a cube-shaped parcel
A parcel's volume is known, and the dispatch team believes the box is a cube. Recover the edge length from that volume before checking the parcel against a shelf opening.
class Main {
public static void main(String[] args) throws Exception {
double volume = 125.0;
System.out.printf(java.util.Locale.ROOT, "%.3f%n", (double) (Math.cbrt(volume)));
}
}
5.000
A parcel holding 125 cubic centimetres has edges of 5 centimetres.
Size a cube-shaped storage bin
The volume of a cube-shaped package is known, but the shelf is labeled by edge length. Recover the length of one edge to see how much horizontal and vertical room the package needs.
class Main {
public static void main(String[] args) throws Exception {
double volume = 216.0;
System.out.printf(java.util.Locale.ROOT, "%.3f%n", (double) (Math.cbrt(volume)));
}
}
6.000
A cube six centimetres on each edge holds 216 cubic centimetres.
Choose the edge of a gift box
A design class models a cube with an internal volume of 64 cubic centimetres. The cutting plan starts with the length of one edge.
Four identical centimetre units along each dimension give 64 cubic centimetres.
What to watch for
Use consistent units. Cubic centimetres give a result in centimetres.