From bf37133f56750bd0dcaabc68bb4c5bbe0e01d605 Mon Sep 17 00:00:00 2001 From: rdevarakonda Date: Mon, 23 Dec 2019 00:51:19 +0000 Subject: [PATCH] [BAEL-3152] - Examples for the article 'Using the Not Operator in If (#8368) Conditions in Java' --- .../operators/notoperator/NotOperator.java | 99 +++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 core-java-modules/core-java-lang-syntax-2/src/main/java/com/baeldung/core/operators/notoperator/NotOperator.java diff --git a/core-java-modules/core-java-lang-syntax-2/src/main/java/com/baeldung/core/operators/notoperator/NotOperator.java b/core-java-modules/core-java-lang-syntax-2/src/main/java/com/baeldung/core/operators/notoperator/NotOperator.java new file mode 100644 index 0000000000..4e0cf98ff2 --- /dev/null +++ b/core-java-modules/core-java-lang-syntax-2/src/main/java/com/baeldung/core/operators/notoperator/NotOperator.java @@ -0,0 +1,99 @@ +package com.baeldung.core.operators.notoperator; + +/** + * Examples used in the article `Using the Not Operator in If Conditions in Java`. + */ +public class NotOperator { + + public static void ifElseStatementExample() { + boolean isValid = true; + + if (isValid) { + System.out.println("Valid"); + } else { + System.out.println("Invalid"); + } + } + + public static void checkIsValidIsFalseWithEmptyIfBlock() { + boolean isValid = true; + + if (isValid) { + + } else { + System.out.println("Invalid"); + } + } + + public static void checkIsValidIsFalseWithJustTheIfBlock() { + boolean isValid = true; + + if (isValid == false) { + System.out.println("Invalid"); + } + } + + public static void checkIsValidIsFalseWithTheNotOperator() { + boolean isValid = true; + + if (!isValid) { + System.out.println("Invalid"); + } + } + + public static void notOperatorWithBooleanValueAsOperand() { + System.out.println(!true); // prints false + System.out.println(!false); // prints true + System.out.println(!!false); // prints false + } + + public static void applyNotOperatorToAnExpression_example1() { + int count = 2; + + System.out.println(!(count > 2)); // prints true + System.out.println(!(count <= 2)); // prints false + } + + public static void applyNotOperatorToAnExpression_LogicalOperators() { + boolean x = true; + boolean y = false; + + System.out.println(!(x && y)); // prints true + System.out.println(!(x || y)); // prints false + } + + public static void precedence_example() { + boolean x = true; + boolean y = false; + + System.out.println(!x && y); // prints false + System.out.println(!(x && y)); // prints true + } + + public static void pitfalls_ComplexConditionsExample() { + int count = 9; + int total = 100; + + if (!(count >= 10 || total >= 1000)) { + System.out.println("Some more work to do"); + } + } + + public static void pitfalls_simplifyComplexConditionsByReversingLogicExample() { + int count = 9; + int total = 100; + + if (count < 10 && total < 1000) { + System.out.println("Some more work to do"); + } + } + + public static void exitEarlyExample() { + boolean isValid = false; + + if(!isValid) { + throw new IllegalArgumentException("Invalid input"); + } + // Code to execute when isValid == true goes here + } +} \ No newline at end of file