# Java Program to Find Next Prime Number

In this tutorial, we will be heading towards solving the Java program to find the next prime number.

Also, I will be looking for all the required packages and methods for its easy implementation.

## Find Next Prime Number in Java

While solving this problem, there are various solutions present that can be implemented.

We will be heading towards solving this problem with the help of nextProbablePrime, a method for checking prime numbers.

Initially, we will be importing util and math package for all our required methods while solving this problem.

Inside the class NextPrime, a static method named nextPrime is created accepting a long value from the main method.

Now comes, BigInteger which is a pre-defined class in java, which is generally used in problems that handle a very large amount of values.

A reference of the above-mentioned class is created and inside the BigInteger constructor, a value which we caught was passed to the method String.valueOf.

Now discussing the above-mentioned method, it basically converts different valued datatypes to a string value.

We can convert int to string, float to string and so on.

Now, after the above execution, we will be returning the next prime value through the reference of BigInteger.

For the purpose of getting the next prime value, as discussed we will be using the method nextProbablePrime.

This above-mentioned method generally returns the next greater prime value than the passed value.

Also, after passing the result, to the main method, we will be getting our desired results.

Below is the program for execution of the above problem of getting the next prime value in java:

```import java.util.*;
import java.math.*;
class NextPrime
{
static long nextPrime(long n)
{
BigInteger b = new BigInteger(String.valueOf(n));
return Long.parseLong(b.nextProbablePrime().toString());
}
public static void main (String[] args) throws java.lang.Exception
{
long n = 28;
System.out.println(nextPrime(n));
}
}```

After the successful implementation of the above program, we get our desired outputs.

Also, by checking with different values passing from the main method, we can check for executing.

Below is sample output for the above program: 