I am currently studying and learning Java.
Now I had to hand in a delivery and used Random.nextInt(int origin, int bound)
(java.Util.Random)
.
Now it was reported back to me that my program is not compilable. (probably it was compiled with too low a version by the tester?). And that's why I searched the internet for the documentation of Random.
https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/Random.html
https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/random/RandomGenerator.html
Random
inherits from RandomGenerator
and therefore has access to nextInt(int origin, int bound)
but in the Random
class documentation nextInt(int bound)
is explicitly mentioned but nextInt(int origin, int bound)
is not. Is this because the former is overwritten by Random
?
CodePudding user response:
RandomGenerator
which is an interface not a class. This interface has added in Java 17 (JEP-356). So if you are shipping your code and it is being compiled with an older version of Java it won't work and lead to non-compilation errors.
So instead of using the newest version of Java I would check which version is the desired one to use.
The specific method you are using is a default method on the RandomGenerator
and isn't directly implemented in Random
because of this. The link to the method is still mentioned in the javadoc of Random
as a referenced method.
CodePudding user response:
It is shown but not the way you think it is.
Take a look at the Methods declared in interface java.util.random.RandomGenerator
section?
Methods declared in interface java.util.random.RandomGenerator isDeprecated, nextDouble, nextDouble, nextExponential, nextFloat, nextFloat, nextGaussian, nextInt, nextLong, nextLong
In this section, nextInt
is a link to RandomGenerator#nextInt
with two int
-parameters.
The docunentation does not mention all methods the class has in the All methods
-section but only declared methods (not inherited methods that are not overridden).