Cleanest Way to Toggle a Boolean Variable in Java

Cleanest way to toggle a boolean variable in Java?

theBoolean = !theBoolean;

What is the quickest way to flip a Java boolean?

I measured with the following code.

public static void main(String[] args)
{

boolean myVariable = true;
long startTime = 0;
long endTime = 0;
long duration1 = 0;
long duration2 = 0;

for(int i=0; i<1000; i++) {
startTime = System.nanoTime();
myVariable = !myVariable;
endTime = System.nanoTime();

duration1 += (endTime - startTime);

startTime = System.nanoTime();
myVariable ^= myVariable;
endTime = System.nanoTime();

duration2 += (endTime - startTime);

}

System.out.println("The duration for the first operation is :" + (duration1/1000));
System.out.println("The duration for second operation is :" + (duration2/1000));
}

and the results are (in nanoseconds)

The duration for the first operation is :140

The duration for the second operation is :123

Based on this analysis, the boolean ^= boolean is quicker than the boolean = !boolean.

What's the most concise way to get the inverse of a Java boolean value?

Just assign using the logical NOT operator ! like you tend to do in your condition statements (if, for, while...). You're working with a boolean value already, so it'll flip true to false (and vice versa):

myBool = !myBool;

Reverse a boolean value?

You can use ! to flip its value.

isTrue = !isTrue;

! inverts the value of a boolean.

Is there an elegant way to rename Java boolean values to improve the readability of my code?

As a simple way, maybe defining constants for TRUE and FALSE is fine? Define them in your class like:

public static final boolean IN = true;
public static final boolean OUT = false;

Otherwise you're pretty limited with Java. You could also try enum:

public enum Anim {
IN(true),
OUT(false);

public final boolean state;

private Element(boolean state) {
this.state = state;
}
}

How to toggle a boolean?

bool = !bool;

This holds true in most languages.

returning boolean variable or returning condition both are same?

There is really no difference here. That variable lives on the stack, so does the value that is returned directly.

So, theoretically, there might be minor minor performance differences between them.

But rest assured: readability is much more important here, therefore I am with you: you can use such an additional variable when it helps the reader. But when you follow clean code principles, another option would be to have a method that only computes that condition and returns the result.

Please note: the "common" practice is to avoid additional variables, so many tools such as PMD or even IDEs suggest you to directly return (see here for a discussion of this aspect).

And finally, coming back on performance. If your method is invoked often enough, the JIT will inline/compile it anyway, and optimize it. If the method isn't invoked often enough, what would we care about a nanosecond more or less of execution time ...



Related Topics



Leave a reply



Submit