"is there a difference between using a logical operator or a bitwise operator in an if block in java?" Code Answer


the logical operator works on booleans, and the bitwise operator works on bits. in this case, the effect is going to be the same, but there are two differences:

  1. the bitwise operator is not meant for that, which makes it harder to read but most importantly
  2. the logical or operator will evaluate the first condition. if it's true, it does not matter what the next condition results in, the result will be true, so the second clause is not executed

here's some handy code to prove this:

public class operatortest {

    public static void main(string[] args){
        system.out.println("logical operator:");
        if(sayandreturn(true, "first") || sayandreturn(true, "second")){

        system.out.println("bitwise operator:");
        if(sayandreturn(true, "first") | sayandreturn(true, "second")){

    public static boolean sayandreturn(boolean ret, string msg){
        return ret;
By Khaled Lela on September 14 2022
Only authorized users can answer the Search term. Please sign in first, or register a free account.