Low Orbit Flux Logo 2 D

Java How To Do Exponents

Exponents are easy in Java. You can just use the pow() method in the java.lang.Math class.

Here is an example showing how this can be done.

import java.lang.Math;

public class Test1 {
    public static void main( String args[] ) {
        double a = 123;        // value
        double exp = 2;        // raise to this power
        double b = Math.pow(a, exp);  
        System.out.println( "Answer: " + b );

You could also do these manually if you want but it generally isn’t worth the effort. They can be done iteratively or recursively.