& vs && and | vs || Java Boolean Operators - Java @ Desk

Monday, March 10, 2014

& vs && and | vs || Java Boolean Operators

& vs && and | vs || Java Boolean Operators

&& and || are the logical operators in Java

& and | are the bitwise operator in java

& and | evaluates both the sides of the equation

&& evaluates the left side and if it is true it goes forward to check the right side. If left side is false, it does not executes the right side. Wherease in case of &, it evaluates left side then moves to right side.

Same is the case with bitwise |

Integer bank_balance = 10000;
Integer loan_emi = 5000;

if(bank_balance > 0 && bank_balance > loan_emi) {..}. In this case it evalues left condition. If it is true, then only it will evaluate the right hand condition. If left is false, it will not evaluate the right.

if(bank_balance < 0 && bank_balance > loan_emi) {..}. In this case it evalues left condition and irrespective of its value it evaluates the right.

Same is the case with bitwise OR






No comments:

Post a Comment