stackoverflow
stackoverflow

Reputation: 409

What does this Java expression mean?

I am pressed for time and thought will post my query here. I'm new to Java and this should be very elementary but I could not get any answer from Google.

What does the second line below mean?

double mutatePercent = 0.01;
boolean m1 = rand.nextFloat() <= mutatePercent;

I thought <= meant less than or equal to, but that doesn't seem so in the above usage. How is m1's value decided?

Thanks!

Upvotes: 0

Views: 147

Answers (3)

Bohemian
Bohemian

Reputation: 425033

The line

boolean m1 = rand.nextFloat() <= mutatePercent;

is of the form

boolean m1 = <some boolean value>

and

rand.nextFloat() <= mutatePercent;

results in a boolean value, so the result of the comparison rand.nextFloat() <= mutatePercent is assigned to the boolean variable m1

Upvotes: 4

Andrew Cooper
Andrew Cooper

Reputation: 32576

That's exactly what it means. The second line performs the "less-than-or-equal" test and assigns the result - ie. true or false - to the boolean variable m1.

Upvotes: 0

Matt Ball
Matt Ball

Reputation: 359826

I thought <= meant less than or equal to

It does.

but that doesn't seem so in the above usage.

Why not?


boolean m1 = rand.nextFloat() <= mutatePercent;

Assigns the value of the condition "Is this random float less than or equal to 0.01?" to the variable m1. So, m1 is true iff the randomly generated number is less than or equal to 0.01.

Upvotes: 0

Related Questions