Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the pipe character do in a Java method call?

I've seen the pipe character used in method calls in Java programs.

For example:

public class Testing1 {

    public int print(int i1, int i2){
        return i1 + i2; 
    }
    public static void main(String[] args){
        Testing1 t1 = new Testing1();
        int t3 = t1.print(4, 3 | 2);
        System.out.println(t3);
    }
}

When I run this, I simply get 7.

Can someone explain what the pipe does in the method call and how to use it properly?

like image 686
CodyBugstein Avatar asked May 08 '13 14:05

CodyBugstein


People also ask

Is pipe a special character in Java?

Splitting a String on delimiter as the pipe is a little bit tricky becuase the most obvious solution will not work, given pipe is a special character in Java regular expression.

What does a vertical line mean in Java?

What does the vertical line mean in Java? In many programming languages, the vertical bar is used to designate the logic operation or, either bitwise or or logical or. … In regular expression syntax, the vertical bar again indicates logical or (alternation).

How do you escape a pipe character in Java?

use \\| instead of | to escape it.

How do you escape a pipe in regex Java?

In regex \ is also used to escape special characters to make them literals like \+ \* . So to escape | in regex we need \| but to create string representing such text we need to write it as "\\|" .


1 Answers

The pipe in 3 | 2 is the bitwise inclusive OR operator, which returns 3 in your case (11 | 10 == 11 in binary).

like image 121
assylias Avatar answered Sep 19 '22 05:09

assylias