1+ package com .baeldung .core .operators .notoperator ;
2+
3+ /**
4+ * Examples used in the article `Using the Not Operator in If Conditions in Java`.
5+ */
6+ public class NotOperator {
7+
8+ public static void ifElseStatementExample () {
9+ boolean isValid = true ;
10+
11+ if (isValid ) {
12+ System .out .println ("Valid" );
13+ } else {
14+ System .out .println ("Invalid" );
15+ }
16+ }
17+
18+ public static void checkIsValidIsFalseWithEmptyIfBlock () {
19+ boolean isValid = true ;
20+
21+ if (isValid ) {
22+
23+ } else {
24+ System .out .println ("Invalid" );
25+ }
26+ }
27+
28+ public static void checkIsValidIsFalseWithJustTheIfBlock () {
29+ boolean isValid = true ;
30+
31+ if (isValid == false ) {
32+ System .out .println ("Invalid" );
33+ }
34+ }
35+
36+ public static void checkIsValidIsFalseWithTheNotOperator () {
37+ boolean isValid = true ;
38+
39+ if (!isValid ) {
40+ System .out .println ("Invalid" );
41+ }
42+ }
43+
44+ public static void notOperatorWithBooleanValueAsOperand () {
45+ System .out .println (!true ); // prints false
46+ System .out .println (!false ); // prints true
47+ System .out .println (!!false ); // prints false
48+ }
49+
50+ public static void applyNotOperatorToAnExpression_example1 () {
51+ int count = 2 ;
52+
53+ System .out .println (!(count > 2 )); // prints true
54+ System .out .println (!(count <= 2 )); // prints false
55+ }
56+
57+ public static void applyNotOperatorToAnExpression_LogicalOperators () {
58+ boolean x = true ;
59+ boolean y = false ;
60+
61+ System .out .println (!(x && y )); // prints true
62+ System .out .println (!(x || y )); // prints false
63+ }
64+
65+ public static void precedence_example () {
66+ boolean x = true ;
67+ boolean y = false ;
68+
69+ System .out .println (!x && y ); // prints false
70+ System .out .println (!(x && y )); // prints true
71+ }
72+
73+ public static void pitfalls_ComplexConditionsExample () {
74+ int count = 9 ;
75+ int total = 100 ;
76+
77+ if (!(count >= 10 || total >= 1000 )) {
78+ System .out .println ("Some more work to do" );
79+ }
80+ }
81+
82+ public static void pitfalls_simplifyComplexConditionsByReversingLogicExample () {
83+ int count = 9 ;
84+ int total = 100 ;
85+
86+ if (count < 10 && total < 1000 ) {
87+ System .out .println ("Some more work to do" );
88+ }
89+ }
90+
91+ public static void exitEarlyExample () {
92+ boolean isValid = false ;
93+
94+ if (!isValid ) {
95+ throw new IllegalArgumentException ("Invalid input" );
96+ }
97+ // Code to execute when isValid == true goes here
98+ }
99+ }
0 commit comments