Last Updated: February 25, 2016
·
26.06K
· safy

Difference Between | (Single Pipe) and ||

The difference is that the | (single pipe) is bitwise or operator and when it can also be used in condition design.

for eg.

class CheckCondition{

public static boolean checkMe(int x)
{
    System.out.println("checked");

        if(x>10)
              return true;
    else 
      return false;
}
   public static void main(String[] args)
{
    int a=2,b=32,c=4;

         if( checkMe(2) | checkMe(2) | checkMe(4)){
        System.out.println("====");
    }

    if(checkMe(2) || checkMe(2) || checkMe(4)){
        System.out.println("====");
    }
}

}

In above code snippet " | " will check every part of condition while " || " will check in sequence starting from first. If any condition in sequence is found to be true then || stops further checking. so || is more efficient in conditional statements.

1 Response
Add your response

its really helpful for me

over 1 year ago ·