When studying my Java courses, these two signs ||
appeared but I still can't find their function or meaning. Can someone clear this up for me?

- 61
- 1
- 1
- 1
-
6http://docs.oracle.com/javase/tutorial/java/nutsandbolts/opsummary.html – Smern Jul 23 '14 at 20:02
-
3It mean or, as in `if (x > 0 || y > 0)`. – Eran Jul 23 '14 at 20:02
-
It's the boolean `or` operator. – TNT Jul 23 '14 at 20:02
-
http://stackoverflow.com/questions/20207997/the-difference-between-and – user2864740 Jul 23 '14 at 20:04
-
1@HotLicks Even if you open up the Java tutorial, it's not that easy to find unless you sit down to read the whole thing with no idea when you'll find it (which I'd call an unreasonable expectation of research effort) or know it's called an operator and that's what you're looking for, also unreasonable to assume of a beginner. – Michelle Jul 23 '14 at 20:13
-
Why these many downvotes ?? Looks like a valid question to me. Where is SO going. Come on guys. It won't deserve -7. – Suresh Atta Jul 23 '14 at 20:14
-
@Michelle - Not unreasonable at all. The OP could well afford to spend 30-60 minutes browsing through "Idiot's Guide to Java" or some such. (Of course, doing a long-winded online tutorial, OTOH, is foolish, until one has skimmed through the basics in reference form. With a tutorial you're just parroting what you see on the page without actually learning anything.) – Hot Licks Jul 23 '14 at 20:18
6 Answers
It means Logical-OR or simply OR --- A or B
A || B
A B A||B
T F T
T T T
F T T
F F F
T = true
F = false
If A is true, it doesn't evaluate B. A || B in this case it is automatically true.

- 331
- 1
- 3
- 17
It means OR, but with the additional feature that if the left operand is TRUE, it doesn't try to figure out the right one. This is called a short-circuiting operation (or sometimes a McCarthy operation). This is very important, because there are times when trying to evaluate the right operand will throw an exception. If s
is a String
:
if (s.length() == 0 || s.charAt(0) == ' ')
If s
is ""
and thus has length 0, s.charAt(0)
will throw an exception and abort your program if it isn't caught. But since ||
is short-circuiting, the left side will be true
if s
is ""
, and therefore it will never try to compute s.charAt(0)
.

- 31,309
- 3
- 58
- 84
||
is a logical OR operator in Java. In addition to above answers one important point to keep in mind for using ||
is short circuit evaluation.
So if you have expression1 || expression2
expression2 will not be evaluated if expression1 is evaluated to be true.

- 66,731
- 38
- 279
- 289
||
means logical OR.
You can read about all the Java operators in the Java Tutorials.

- 45,603
- 8
- 97
- 119
It is the boolean operator or. That means it will take two terms and compare them, and if either one or both are true, it will return true. However, if neither are true, it will return false. Example:
return (true || false);
will return true.

- 1,016
- 9
- 18
It is a logical operator used in java.It is usually known as OR operator. for example `
if(a==1 || b==1) System.out.println("Something");
` if the value of variable 'a' is 1 or value of variable 'b' is 1 it will print "Something".If one of the value is true it will print Something.If both the values are false it will not print anything.

- 78
- 1
- 6