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
This commit is contained in:
sumit-bhawsar 2019-12-24 17:03:31 +00:00 committed by Sam Millington
parent fe17c22c81
commit 6fd77ddd65
2 changed files with 72 additions and 0 deletions

View File

@ -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/)

View File

@ -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);
}
}