Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Java 8 why we cannot convert Math.random() to Math::random using method references

This works

Supplier<Double> random1 = () -> Math.random();

Why this doesn't work:

Supplier<Double> random2 = () -> Math::random
like image 950
san1deep2set3hi Avatar asked Jan 26 '26 03:01

san1deep2set3hi


1 Answers

Using just the method reference would work such as:

Supplier<Double> random2 = Math::random

since it already represents a Supplier<Double>.

On the other hand, the current notation that you've used would represent a Supplier of Supplier<Double> such as:

Supplier<Supplier<Double>> random2 = () -> Math::random;
like image 195
Naman Avatar answered Jan 27 '26 17:01

Naman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!