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?
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 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).
use \\| instead of | to escape it.
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 "\\|" .
The pipe in 3 | 2
is the bitwise inclusive OR operator, which returns 3 in your case (11 | 10 == 11
in binary).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With