Java Tutorial: The Power Function

Xah Lee, 2005-01

It's interesting that java doesn't provide the power operator. (for example, 3^4). You have to use “java.lang.Math.pow(3,4)”. That method returns type “double”.

The matching of types in java is quite a pain.

import java.lang.Math;

class t2 {
    public double square (int n) {
        return java.lang.Math.pow(n,2);
    }
}

class t1 {
    public static void main(String[] arg) {
                t2 x1 = new t2();
                double m =x1.square(3);
                System.out.println(m);
    }
}

Reference: Java Doc: Math↗.


Page created: 2005-01.
© 2005 by Xah Lee.
Xah Signet