Delphi Clinic C++Builder Gate Training & Consultancy Delphi Notes Weblog Dr.Bob's Webshop
Hubert Klein Ikkink (aka Mr.Haki) - Communications Officer
 Mr.Haki's JBuilder Jar #19
See Also: JBuilder Papers

Find the power!
When we are using arithmetic operators in our Java programming, we might be wondering how to raise the power of a certain number to a certain number. The only available operators are: +, -, *, /, %, to add, substract, multiply, divide, or compute the remainder.
In other programming languages we can use the following symbol: ^, to raise a number by the power of another number. But Java doesn't allow the use of this symbol, so how do we compute the power?

The answer can be found in the java.lang.Math class. This class contains a method with the name pow(). And this method will do exactly what we want to do.

Let's use this method in a small example application:

  public class ComputePower {
    public static void main(String[] args) {
      /*
       * Show result of 2 raised by the power of 4 (will be 16)
       */
      System.out.println(Math.pow(2, 4));
    }
  }

This webpage © 1997-2009 by Bob Swart (aka Dr.Bob - www.drbob42.com). All Rights Reserved.