Java Guava | isPrime() method of IntMath Class
The isPrime(int n) method of Guava’s IntMath class is used to check whether the parameter passed to it is a prime number or not. If the parameter passed to it is prime, then it returns True otherwise it returns False.
A number is said to be Prime if it is divisible only by 1 and the number itself.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
public static boolean isPrime(int n)
Parameter: The method accepts only one parameter n which is of integer type and is to be checked for primality.
Return Value :
- true : if n is a prime number.
- false : if n is 0, 1 or composite number.
Exceptions : The method isPrime(int n) throws IllegalArgumentException if n is negative.
Example 1 :
63 is not a prime number 17 is a prime number
Example 2 :
java.lang.IllegalArgumentException: n (-7) must be >= 0