How can I calculate the power of an integer in Java without using doubles?

If you want to compute integer powers without using Math.pow(), the simplest way is to use a loop:

public static int power(int base, int exponent) {
    int result = 1;
    for (int i = 0; i < exponent; i++) {
        result *= base;
    }
    return result;
}

Things to note:

  • :white_check_mark: Works for non-negative exponents
  • :x: Not efficient for large exponents