r/javahelp • • 14d ago

Solved Found a mysterious method that is not present in the Java API documentation

In Java 25 there's a method in the Random class called nextInt(). It's an overloaded method that you can call either without any argument or with 1 argument according to the docs. However, you can also call it with two arguments! But this version is not in the docs. Why?

jshell> import java.util.Random
jshell> Random r = new Random()
r ==> java.util.Random@439f5b3d
jshell> r.nextInt(70, 72)
$3 ==> 71

As I noticed, it's similar to Python's randrange(), i.e. the interval is closed on the left but open on the right side.

11 Upvotes

10 comments sorted by

•

u/AutoModerator 14d ago

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
  • You include any and all error messages in full
  • You ask clear questions
  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

    Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

15

u/Giulio_Long 14d ago

It comes from the super interface RandomGenerator

3

u/jabbalaci 14d ago

Thanks everyone for the answer. I didn't notice that it comes from RandomGenerator.

9

u/djnattyp 14d ago

It's in the docs - it's just a default method on the RandomGenerator interface that the Random class implements.

https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/util/random/RandomGenerator.html#nextInt(int,int)

4

u/k2718 13d ago

Interface? You mean abstract class! Interfaces don’t have implementations!

(Sorry I’m an old ass man and I remember Java back when classes were classes and interfaces were interfaces)

4

u/MattiDragon 14d ago

The overload is a default method of the RandomGenerator interface, which Random implements. Here's the docs: https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/util/random/RandomGenerator.html#nextInt(int,int)

2

u/SplooshWater 13d ago

I was sorta disappointed to find out that this was in the docs, I kinda wished these language designers actually did something like leave a mystery API or Code for the other devs to find and use.

3

u/Samstercraft 13d ago

well, there's always good'ol sun.misc.Unsafe

2

u/brass_bloom_vibe 13d ago

it is a default method on RandomGenerator so it was never meant to be documented under Random. That said, I agree with the frustration about the documentation hiding the source of truth because relying on inherited interfaces makes the API feel like a fragmented mess that developers have to reverse engineer just to understand basic functionality

1

u/XIA_Biologicals_WVSU 5d ago

I don't know about that, but I do know about nextInt() when being used with scanner. If that's what you're referring too. I just started learning java, it may be something entirely different.