From 655c5458050a06a87d16381693b3b29384a96a37 Mon Sep 17 00:00:00 2001 From: sumit-bhawsar <56178733+sumit-bhawsar@users.noreply.github.com> Date: Tue, 24 Dec 2019 17:03:31 +0000 Subject: [PATCH] Bael 3512-difference between logical and bitwise & (#8203) * BAEL-3512 unit tests for Bitwise and Logical AND operators * BAEL-3512 readme.md is updated and added new tests for bitwise & with booleans * BAEL-3512 updated variables names to more meaningful names * BAEL-3512 added example for short circuit --- .../core-java-lang-operators/README.md | 1 + ...BitwiseAndLogicalANDOperatorsUnitTest.java | 71 +++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 core-java-modules/core-java-lang-operators/src/test/java/com/baeldung/andoperators/BitwiseAndLogicalANDOperatorsUnitTest.java diff --git a/core-java-modules/core-java-lang-operators/README.md b/core-java-modules/core-java-lang-operators/README.md index 1fe524948c..3af2c8885b 100644 --- a/core-java-modules/core-java-lang-operators/README.md +++ b/core-java-modules/core-java-lang-operators/README.md @@ -11,4 +11,5 @@ This module contains articles about Java operators - [Java Compound Operators](https://www.baeldung.com/java-compound-operators) - [The XOR Operator in Java](https://www.baeldung.com/java-xor-operator) - [Java Bitwise Operators](https://www.baeldung.com/java-bitwise-operators) +- [Bitwise & vs Logical && Operators](https://www.baeldung.com/bitwise-vs-logical-operators/) diff --git a/core-java-modules/core-java-lang-operators/src/test/java/com/baeldung/andoperators/BitwiseAndLogicalANDOperatorsUnitTest.java b/core-java-modules/core-java-lang-operators/src/test/java/com/baeldung/andoperators/BitwiseAndLogicalANDOperatorsUnitTest.java new file mode 100644 index 0000000000..6158c2870d --- /dev/null +++ b/core-java-modules/core-java-lang-operators/src/test/java/com/baeldung/andoperators/BitwiseAndLogicalANDOperatorsUnitTest.java @@ -0,0 +1,71 @@ +package com.baeldung.andoperators; + +import org.junit.jupiter.api.Test; + +import static org.junit.Assert.*; + +public class BitwiseAndLogicalANDOperatorsUnitTest { + + @Test + public void givenTwoTrueBooleans_whenBitwiseAndOperator_thenTrue() { + boolean trueBool = true; + boolean anotherTrueBool = true; + boolean trueANDTrue = trueBool & anotherTrueBool; + assertTrue(trueANDTrue); + } + + @Test + public void givenOneFalseAndOneTrueBooleans_whenBitwiseAndOperator_thenFalse() { + boolean trueBool = true; + boolean falseBool = false; + boolean trueANDFalse = trueBool & falseBool; + assertFalse(trueANDFalse); + } + + @Test + public void givenTwoFalseBooleans_whenBitwiseAndOperator_thenFalse() { + boolean falseBool = false; + boolean anotherFalseBool = false; + boolean falseANDFalse = falseBool & anotherFalseBool; + assertFalse(falseANDFalse); + } + + @Test + public void givenTwoIntegers_whenBitwiseAndOperator_thenNewDecimalNumber() { + int six = 6; + int five = 5; + int shouldBeFour = six & five; + assertEquals(4, shouldBeFour); + } + + @Test + public void givenTwoTrueBooleans_whenLogicalAndOperator_thenTrue() { + boolean trueBool = true; + boolean anotherTrueBool = true; + boolean trueANDTrue = trueBool && anotherTrueBool; + assertTrue(trueANDTrue); + } + + @Test + public void givenOneFalseAndOneTrueBooleans_whenLogicalAndOperator_thenFalse() { + boolean trueBool = true; + boolean falseBool = false; + boolean trueANDFalse = trueBool && falseBool; + assertFalse(trueANDFalse); + } + + @Test + public void givenTwoFalseBooleans_whenLogicalAndOperator_thenFalse() { + boolean falseBool = false; + boolean anotherFalseBool = false; + boolean falseANDFalse = falseBool && anotherFalseBool; + assertFalse(falseANDFalse); + } + + @Test + public void givenTwoFalseExpressions_whenLogicalAndOperator_thenShortCircuitFalse() { + boolean shortCircuitResult = (2<1) && (4<5); + assertFalse(shortCircuitResult); + } + +}