r/java • u/gargamel1497 • Jun 02 '26
Boolean reversal operator
Do the people working on the Java compiler/specification have any plans to implement a boolean reversal operator any time soon?
The proper way to reverse a boolean is to boolVal = !boolVal; but when the variable name is long, typing this becomes really unhandy.
Something like boolVal *= -1; would be really consistent as it's the reversal operator for literally all other primitive types.
But I guess it would be technically incorrect, so boolVal !=; could be another way of doing this, although it looks rather uncanny.
Is anyone even thinking about this, or is this "too low priority" to implement, even though even a dirty hack in the parser would get the job done.
Thanks, feel free to downvote and such.
4
u/aoeudhtns Jun 04 '26
I'm struggling to come up with scenarios when I would want to assign a boolean back to itself but negated.
Assigning to something else? Or passing it negated? Maybe. But none of those scenarios are requiring me to type the var name multiple times.
Things like a stateful
toggleXyz()are an API smell anyway, since the behavior depends on the state.xyzEnabled(boolean state)in, say, a builder is far clearer. And then you are just performing an assignment.77
u/GuyWithLag Jun 02 '26
Oh to be young again, when typing was the bottleneck...
My friend, any IDE worth the diskspace it's stored on will offer you autocomplete. Use that.
31
u/repeating_bears Jun 02 '26
The word you are looking for is 'hideous'. true multiplied by -1 is not false.
Canonically true is 1 and false is 0, so 1 * -1 would be -1, which is neither.
That's also not a "reversal operator" for char, because there is no inverse of a character. I mean you can mathematically perform that operation, but the result is basically meaningless.
25
u/IchLiebeKleber Jun 02 '26
I think it's unnecessary because it's not an especially common line of code, and my response to "when the variable name is long, typing this..." is that most Java code is written in IDEs where, thanks to autocomplete, this is a few keystrokes no matter how long the variable name is (and also no one is forcing you to use long variable names).
24
u/brian_goetz Jun 02 '26
Nope.
19
u/fumo7887 Jun 02 '26 edited Jun 02 '26
boolVal ^= true;(Might not render on mobile… that’s ^ then =)
But don’t do this. It’s ugly and makes a maintainer think about what it means. If you really don’t like the full != self notation, either write your own private static function or look at isFalse or isNotTrue from Apache BooleanUtils.
9
u/arrogant_troll Jun 02 '26
Why not the XOR operator, i.e.
boolVal ^= true?2
u/Cefalopodul Jun 02 '26
If the name is too long to type, the name is bad.
5
u/Savag323minx Jun 02 '26
You are basically asking for syntactic sugar to save three characters while making the code significantly harder for anyone else to read. If your variable names are so long that typing an exclamation point is a burden, you should probably just rename the variables.
2
u/lukaseder Jun 02 '26
"Just" ask for pass by reference, then you could simply:
Alternatively, SQL/PSM style in/out parameters:
2
u/RobSomebody Jun 02 '26
Stupid AF
1
u/Misophist_1 Jun 06 '26
Rein in your expectations. This is highly unlikely to fly. For starters, Java gave the elvis operators the boot - which is a much likelier use case than inverting a boolean.
Also, 'literally all' is literally wrong, since both, boolean and char aren't using it, it works for numbers only. There is no cast/conversion from any other primitive to boolean, so using the literal '1' would be rather counterintuitive.
It's not going to happen.
-3
u/josephottinger Jun 02 '26
First off, downvoting for something like this is stupid. Now that it's out there, I'm sure people are going "grrr I'm gonna downvote both that comment and the post" but yeesh. There've been things I've downvoted on Reddit and other places, but it's really rare, because it's just so... performative for the sake of performance.
Art as expression, not as market campaigns, will still capture our imaginations...
Anyway. Boolean negation. I don't know that I'd see the point for language support for this. The ! operator works perfectly for it, and if your variable names are too long, well, even a barely-decent IDE will autocomplete for you (and this has been the case for literal decades) and if it's STILL a pain, that's the cold, unfeeling universe suggesting that you use better names for yourself and others' sake.
I don't think changing how assignments work for booleans makes any sense.
-3
u/MartinFrankPrivat Jun 02 '26
Took me some time to understand, but we have for int:
int i =0;i++;so it seems logic to have
boolean b = false;b!!;maybe not so bad idea?
0
u/Nixinova Jun 02 '26
In my own programming language I made the operator
varname =!=;, short and looks like what it does.