1

In Delphi I can do the following with a boolean variable:

If NOT bValue then
begin
  //do some stuff
end;

Does the equivalent in Java use the !?

If !(bValue) {
  //do some stuff
}
RRUZ
  • 133,023
  • 16
  • 348
  • 476
Leslie
  • 3,494
  • 7
  • 36
  • 50

3 Answers3

10

You're close; the proper syntax is:

if (!bValue) {
  //do some stuff
}

The entire conditional expression must be inside the parenthesis; the condition in this case involve the unary logical complement operator ! (JLS 15.15.6).

Additionally, Java also has the following logical binary operators:

There are also compound assignment operators (JLS 15.26.2) &=, |=, ^=.


Other relevant questions on stackoverflow:

Community
  • 1
  • 1
polygenelubricants
  • 364,035
  • 124
  • 554
  • 617
4

Yes, but inside the bracket:

if (!bValue) {
}

You'd normally not use any sort of data type prefix in Java as well, so it would more likely be something like:

if (!isGreen) { // or some other meaningful variable name
}
David M
  • 69,925
  • 13
  • 153
  • 183
  • Thanks! My variable is actually IsFYE so it is meaningful! – Leslie Apr 06 '10 at 16:29
  • BTW: Using data type prefixes isn't common practice in Delphi either. More common are prefixes that identify the scope of a variable which I generally find much more useful, too. – Oliver Giesen Apr 26 '10 at 09:55
0
if (!bValue) {
    // do some stuff
}
pajton
  • 15,360
  • 7
  • 53
  • 65