nextInt
is a static method of the RandomUtils
class that returns a random integer value.
RandomUtils
RandomUtils
is defined in the Apache Commons Lang
package. Apache Commons Lang
can be added to the Maven project by adding the following dependency to the pom.xml
file.
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
For other versions of the
commons-lang
package, refer to the Maven Repository.
public static int nextInt()
The nextInt
method doesn’t take any parameters.
This method returns a random integer value.
The returned random integer value will be within
0 to Integer.MAX_VALUE
.
In the code below, we use the nextInt
method as follows:
import org.apache.commons.lang3.RandomUtils;class NextIntExample {public static void main( String args[] ) {System.out.println("RandomUtils.nextInt() => " + RandomUtils.nextInt()); // random int valueSystem.out.println("RandomUtils.nextInt() => " + RandomUtils.nextInt()); // random int value}}
In the code above:
RandomUtils
class.import org.apache.commons.lang3.RandomUtils;
nextInt
method to get the random integer value.RandomUtils.nextInt() => 12
RandomUtils.nextInt() => 189
When you run the code, your output can be different because a random number is generated.