Thursday, October 23, 2025
HomeLanguagesJavaJava Guava | LongMath.checkedPow(long b, int k) method with Examples

Java Guava | LongMath.checkedPow(long b, int k) method with Examples

checkedPow(long b, long k) is a method of Guava’s LongMath Class which accepts two parameters b and k and is used to find the k-th power of b.

Syntax:

public static long checkedPow(long b, long k)

Parameters: The method accepts two parameters, b and k. The parameter b is called base which is raised to the k-th power.

Return Value: This method returns the k-th power of b.

Exceptions: The method checkedPow(long b, long k) throws ArithmeticException if the k-th power of b overflows in signed long arithmetic.

Below examples illustrate the implementation of the above method:

Example 1:




// Java code to show implementation of
// checkedPow(long b, long k) method of
// Guava's LongMath class
  
import java.math.RoundingMode;
import com.google.common.math.LongMath;
  
class GFG {
  
    // Driver code
    public static void main(String args[])
    {
        long b1 = 5;
        int k1 = 7;
  
        // Using checkedPow(long b, long k) method
        // of Guava's LongMath class
        long ans1 = LongMath.checkedPow(b1, k1);
  
        System.out.println(b1 + " to the "
                           + k1 + "th power is: "
                           + ans1);
  
        long b2 = 19;
        int k2 = 4;
  
        // Using checkedPow(long b, long k) method
        // of Guava's LongMath class
        long ans2 = LongMath.checkedPow(b2, k2);
  
        System.out.println(b2 + " to the " + k2
                           + "th power is: "
                           + ans2);
    }
}


Output:

5 to the 7th power is: 78125
19 to the 4th power is: 130321

Example 2:




// Java code to show implementation of
// checkedPow(long b, long k) method of
// Guava's LongMath class
  
import java.math.RoundingMode;
import com.google.common.math.LongMath;
  
class GFG {
  
    static long findPow(long b, int k)
    {
        try {
  
            // Using checkedPow(long b, long k) method of
            // Guava's LongMath class
            // This should raise "ArithmeticException" as
            // b to the kth power overflows in
            // signed long arithmetic
            long ans = LongMath.checkedPow(b, k);
  
            // Return the answer
            return ans;
        }
        catch (Exception e) {
            System.out.println(e);
            return -1;
        }
    }
  
    // Driver code
    public static void main(String args[])
    {
        long b = 20;
        int k = 25;
  
        try {
  
            // Function calling
            findPow(b, k);
        }
        catch (Exception e) {
            System.out.println(e);
        }
    }
}


Output:

java.lang.ArithmeticException: overflow

Reference: https://google.github.io/guava/releases/20.0/api/docs/com/google/common/math/LongMath.html#checkedPow-long-int-

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS