Java boolean primitive

Can I do this in java?

private static boolean isRight(){
   return new Random.nextBolean();

boolean test = true;
test = test && isRight()

My question is can I use a boolean to update the same boolean like I would if it were an int or double? Is this a good programming practice or there’s a better way


Yes of course you can. boolean works in the same way as any other primitive type in Java.

Note however that this is an abuse of Random: you should not reinitialise the generator every time you want to draw from it. You could keep it as a static field in the class, taking care to synchronize on its monitor to help achieve thread safety.

Note also that test will remain false once it evaluates to false.

Leave a Reply

Your email address will not be published. Required fields are marked *