This is what I wrote for a coding bat project thing. For some reason it says that this way it doesn't work but if I flip it, it works. Why is that? When it inputs something with less than 3 chars it gets an error message according to codingbat.
// Given a string, return a new string where "not " has been added to the front.
// However, if the string already begins with "not", return the string unchanged.
// Note: use .equals() to compare 2 strings.
// notString("candy") → "not candy"
// notString("x") → "not x"
// notString("not bad") → "not bad"
public String notString(String str) {
String m;
if ( str.substring (0,3).equalsIgnoreCase("not") && str.length () >= 3 ) // this line doesn't work in it's current state
// but works if I flip the two boolean expressions over the &&
m = str;
else {
m = "not " + str;
}
return m;
If the length of the string is not at least 3, then str.subtring(0, 3) will fail with a IndexOutOfBoundsException.
The reason that it works when flipped is called short circuit evaluation. Flipped:
if ( str.length() >= 3 && str.substring (0,3).equalsIgnoreCase("not") )
The first condition is evaluated. If it's less than 3, then Java knows that the whole condition is false, because false && anything is false. It won't evaluate the other expression, because it doesn't have to evaluate it. The IndexOutOfBoundsException doesn't come for this reason.
The JLS, Section 15.23 talks about this:
The conditional-and operator && is like & (§15.22.2), but evaluates
its right-hand operand only if the value of its left-hand operand is
true.
Additionally, the logical-or operator (conditional-or operator) || works similarly. It will only evaluate its right-side operand if the left-side operand is false (JLS Section 15.24).
The code you posted above will crash with a StringIndexOutOfBoundsException if str is shorter than three characters, because you're trying to take a 3-character substring of a string that's not long enough.
However, when you flip it around, you check the length of the string first. This means that you know right away that the && will fail (because str.length >= 3 is false), so you short-circuit out of the conditional right then and there. As a result, you never try to take the impossible substring, and you avoid the crash.
As mentioned in the link, both of the logical operators work this way (&& (AND) and || (OR)). If they're able to figure out what to return after evaluating only the left-hand side, the right-hand side never gets touched. So, for example, (true || 1/0 == 0) will always evaluate to true, even though if the right-hand side were to be evaluated, it would throw an exception.
It's because you check
str.substring (0,3).equalsIgnoreCase("not")
first, before checking a length. So you propably gen an error java.lang.StringIndexOutOfBoundsException if your str is of length less than 3.
You have to check a length first (e.g. by flipping conditions check).
Related
I know the rules for && and || but what are & and |? Please explain these to me with an example.
Those are the bitwise AND and bitwise OR operators.
int a = 6; // 110
int b = 4; // 100
// Bitwise AND
int c = a & b;
// 110
// & 100
// -----
// 100
// Bitwise OR
int d = a | b;
// 110
// | 100
// -----
// 110
System.out.println(c); // 4
System.out.println(d); // 6
Thanks to Carlos for pointing out the appropriate section in the Java Language Spec (15.22.1, 15.22.2) regarding the different behaviors of the operator based on its inputs.
Indeed when both inputs are boolean, the operators are considered the Boolean Logical Operators and behave similar to the Conditional-And (&&) and Conditional-Or (||) operators except for the fact that they don't short-circuit so while the following is safe:
if((a != null) && (a.something == 3)){
}
This is not:
if((a != null) & (a.something == 3)){
}
"Short-circuiting" means the operator does not necessarily examine all conditions. In the above examples, && will examine the second condition only when a is not null (otherwise the whole statement will return false, and it would be moot to examine following conditions anyway), so the statement of a.something will not raise an exception, or is considered "safe."
The & operator always examines every condition in the clause, so in the examples above, a.something may be evaluated when a is in fact a null value, raising an exception.
I think you're talking about the logical meaning of both operators, here you have a table-resume:
boolean a, b;
Operation Meaning Note
--------- ------- ----
a && b logical AND short-circuiting
a || b logical OR short-circuiting
a & b boolean logical AND not short-circuiting
a | b boolean logical OR not short-circuiting
a ^ b boolean logical exclusive OR
!a logical NOT
short-circuiting (x != 0) && (1/x > 1) SAFE
not short-circuiting (x != 0) & (1/x > 1) NOT SAFE
Short-circuit evaluation, minimal evaluation, or McCarthy evaluation (after John McCarthy) is the semantics of some Boolean operators in some programming languages in which the second argument is executed or evaluated only if the first argument does not suffice to determine the value of the expression: when the first argument of the AND function evaluates to false, the overall value must be false; and when the first argument of the OR function evaluates to true, the overall value must be true.
Not Safe means the operator always examines every condition in the clause, so in the examples above, 1/x may be evaluated when the x is, in fact, a 0 value, raising an exception.
I know there's a lot of answers here, but they all seem a bit confusing. So after doing some research from the Java oracle study guide, I've come up with three different scenarios of when to use && or &.
The three scenarios are logical AND, bitwise AND, and boolean AND.
Logical AND:
Logical AND (aka Conditional AND) uses the && operator. It's short-circuited meaning: if the left operand is false, then the right operand will not be evaluated. Example:
int x = 0;
if (false && (1 == ++x) {
System.out.println("Inside of if");
}
System.out.println(x); // "0"
In the above example the value printed to the console of x will be 0, because the first operand in the if statement is false, hence java has no need to compute (1 == ++x) therefore x will not be computed.
Bitwise AND:
Bitwise AND uses the & operator. It's used to preform a bitwise operation on the value. It's much easier to see what's going on by looking at operation on binary numbers ex:
int a = 5; // 5 in binary is 0101
int b = 12; // 12 in binary is 1100
int c = a & b; // bitwise & preformed on a and b is 0100 which is 4
As you can see in the example, when the binary representations of the numbers 5 and 12 are lined up, then a bitwise AND preformed will only produce a binary number where the same digit in both numbers have a 1. Hence 0101 & 1100 == 0100. Which in decimal is 5 & 12 == 4.
Boolean AND:
Now the boolean AND operator behaves similarly and differently to both the bitwise AND and logical AND. I like to think of it as preforming a bitwise AND between two boolean values (or bits), therefore it uses & operator. The boolean values can be the result of a logical expression too.
It returns either a true or false value, much like the logical AND, but unlike the logical AND it is not short-circuited. The reason being, is that for it to preform that bitwise AND, it must know the value of both left and right operands. Here's an ex:
int x = 0;
if (false & (1 == ++x) {
System.out.println("Inside of if");
}
System.out.println(x); //"1"
Now when that if statement is ran, the expression (1 == ++x) will be executed, even though the left operand is false. Hence the value printed out for x will be 1 because it got incremented.
This also applies to Logical OR (||), bitwise OR (|), and boolean OR (|)
Hope this clears up some confusion.
The operators && and || are short-circuiting, meaning they will not evaluate their right-hand expression if the value of the left-hand expression is enough to determine the result.
& and | provide the same outcome as the && and || operators. The difference is that they always evaluate both sides of the expression where as && and || stop evaluating if the first condition is enough to determine the outcome.
In Java, the single operators &, |, ^, ! depend on the operands. If both operands are ints, then a bitwise operation is performed. If both are booleans, a "logical" operation is performed.
If both operands mismatch, a compile time error is thrown.
The double operators &&, || behave similarly to their single counterparts, but both operands must be conditional expressions, for example:
if (( a < 0 ) && ( b < 0 )) { ... } or similarly,
if (( a < 0 ) || ( b < 0 )) { ... }
source: java programming lang 4th ed
& and | are bitwise operators on integral types (e.g. int): http://download.oracle.com/javase/tutorial/java/nutsandbolts/op3.html
&& and || operate on booleans only (and short-circuit, as other answers have already said).
Maybe it can be useful to know that the bitwise AND and bitwise OR operators are always evaluated before conditional AND and conditional OR used in the same expression.
if ( (1>2) && (2>1) | true) // false!
&& ; || are logical operators.... short circuit
& ; | are boolean logical operators.... Non-short circuit
Moving to differences in execution on expressions. Bitwise operators evaluate both sides irrespective of the result of left hand side. But in the case of evaluating expressions with logical operators, the evaluation of the right hand expression is dependent on the left hand condition.
For Example:
int i = 25;
int j = 25;
if(i++ < 0 && j++ > 0)
System.out.println("OK");
System.out.printf("i = %d ; j = %d",i,j);
This will print i=26 ; j=25, As the first condition is false the right hand condition is bypassed as the result is false anyways irrespective of the right hand side condition.(short circuit)
int i = 25;
int j = 25;
if(i++ < 0 & j++ > 0)
System.out.println("OK");
System.out.printf("i = %d ; j = %d",i,j);
But, this will print i=26; j=26,
If an expression involving the Boolean & operator is evaluated, both operands are evaluated. Then the & operator is applied to the operand.
When an expression involving the && operator is evaluated, the first operand is evaluated. If the first operand evaluates to false, the evaluation of the second operand is skipped.
If the first operand returns a value of true then the second operand is evaluated. If the second operand returns a value of true then && operator is then applied to the first and second operands.
Similar for | and ||.
While the basic difference is that & is used for bitwise operations mostly on long, int or byte where it can be used for kind of a mask, the results can differ even if you use it instead of logical &&.
The difference is more noticeable in some scenarios:
Evaluating some of the expressions is time consuming
Evaluating one of the expression can be done only if the previous one was true
The expressions have some side-effect (intended or not)
First point is quite straightforward, it causes no bugs, but it takes more time. If you have several different checks in one conditional statements, put those that are either cheaper or more likely to fail to the left.
For second point, see this example:
if ((a != null) & (a.isEmpty()))
This fails for null, as evaluating the second expression produces a NullPointerException. Logical operator && is lazy, if left operand is false, the result is false no matter what right operand is.
Example for the third point -- let's say we have an app that uses DB without any triggers or cascades. Before we remove a Building object, we must change a Department object's building to another one. Let's also say the operation status is returned as a boolean (true = success). Then:
if (departmentDao.update(department, newBuilding) & buildingDao.remove(building))
This evaluates both expressions and thus performs building removal even if the department update failed for some reason. With &&, it works as intended and it stops after first failure.
As for a || b, it is equivalent of !(!a && !b), it stops if a is true, no more explanation needed.
Why isn't this an infinite recursive loop?
public static void main(String[] args) {
System.out.println(isSuch(99) + " " + isSuch(100));
}
public static boolean isSuch(int n)
{
System.out.println("n is currently at "+ n);
return n > 2 && !isSuch(n - 2);
}
It is not an infinite recursive loop, because it will eventually stop. It stops because the && operator is a short-circuiting operator. The JLS, Section 15.23, describes the && operator:
The conditional-and operator && is like & (§15.22.2), but evaluates its right-hand operand only if the value of its left-hand operand is true.
When the recursion reaches a level where n is not greater than 2, then the && operator immediately returns false without evaluating the right hand side, which is the recursive call. That effectively makes the case where n is not greater than 2 the base case. Then previous recursive calls invert it with the ! operator, returning true. Both calls return true, and true is printed twice.
It is worth noting that while this is a pretty deep recursion, the stack size was more than enough to handle this. But it's not necessary to be an infinitely recursing loop for a StackOverflowError to occur. All it needs is to recurse far enough. Calling isSuch(99999) is enough to cause a StackOverflowError on my box.
Also, if you had used the non-short-circuiting operator & instead of &&, then it would be an infinitely recursive loop, and a StackOverflowError would occur regardless of what number was originally passed to isSuch.
Short-Circuit Logical Operators
The OR operator results in true when first operand is true, no
matter what the second operand is. The AND operator results in
false when first operand is false, no matter what the second operand
is. If you use the || and &&, Java will not evaluate the
right-hand operand when the outcome can be determined by the left
operand alone.
In your sample code when value of n becomes equal to 2, condition
**n > 2 && !isSuch(n - 2)** immediately becomes false since first operand is false, so next operand will not be evaluated thus isSuch() method will not be called.
&& is a short-circuit operator evaluated from left to right, so if the operand on the left side of && operator is evaluated to false, evaluation should not continue. BUT I expected that the ++ should be evaluated before && because it has higher precedence, and (from the link):
Operators with higher precedence are evaluated before operators with relatively lower precedence.
In that case, why doesn't count increment in the third line of this code?
int mask = 2;
int count = 0;
if( !(mask > 1) && ++count > 1) { mask += 100; }
System.out.println(mask + " " + count);
No, the way the expression is evaluated goes from left to right, while taking operator precedence into account. So once in a && b the a gets false, b is not evaluated anymore.
JLS §15.23. Conditional-And Operator && says:
The conditional-and operator && is like & (§15.22.2), but evaluates its right-hand operand only if the value of its left-hand operand is true.
Here is a detailed step by step evaluation of how this works:
This is because, when you parse an expression as a human being, you start with the lowest precedence operators first. In this case && has lower precedence then ++.
++a < 0 && foo
^^
start
In order to compute the result of &&, you should know the left operand:
++a < 0 && ++b
^^^^^^^
so compute this
Now, when you want to know the result of ++a < 0, take a look at the lowest precedence operator first, which is <:
++a < 0
^
evaluate this
In order to do that, you will need both left and right operand. So compute them:
++a
0
This is the moment were ++a gets increased.
So, now we are at the bottom. Let's make our way back to the top:
false && foo
And this is where the fact that && is short circuit comes in. Left operand is false, so the right operand expression foo is not evaluated anymore. Which becomes:
false
without the evaluation of the right operand.
Edit: I missed the point of the question. See revisions for semi-detailed explanation of short-circuit evaluation.
Each operand of the logical-AND and logical-OR operators is a separate expression, and thus, treated independently (given its own order of operators to evaluate, according to correct operator precedence).
So, while ++ has the highest precedence in ++count > 1, that expression is never even evaluated (because of the short-circuiting).
You seem to be dissatisfied with the short circuit evaluation explanation. Why is precedence not important here?
Imagine this code:
if( !(mask > 1) && Increment(count) > 1)
Function application (that is, calling a function by writing the brackets) is the highest precedence, yet the function call will never happen. If short circuit evaluation made allowances for precedence, it wouldn't really work at all.
To expand a little - operators such as ++ , - , etc all map to function calls. However, the compiler needs to understand the order that they bind. This is not the order that they are evaluated in.
So a + b * c doesn't mean workout b * c then the sum. It means only this add( a, multiply(b, c)) The order of evaluation is different in the case of && (though not in my example), to make it easier for programmers to write quick code.
To avoid confusion, recall that
if (a && b) {
// do something
}
is semantically the same as:
if (a) {
if (b) {
// do something
}
}
Obviously, if a evaluates to false, the term b will never be seen. In particular, an increment opertor there will not execute.
By recalling this expansion, the behaviour should be easier to understand. It's best to consider && as a shortcut for above nested-if statement. (In the case of having an else statement, you will also need to add two copies of the else code, so it's not as easy.)
A more "exact" representation would be to use a condition method:
boolean condition() {
if (!a) return false;
if (!b) return false;
return true;
}
Again, b will never be evaluated if a returns false already.
if( !(mask > 1) && ++count > 1) { mask += 100; }
What this line indicates is:
!(2>1) ie !(true) ie false
In && left side is false it doesnot futher go for checking rightside
so count is not incremented.
The contents of both of the following if blocks should be executed:
if( booleanFunction() || otherBooleanFunction() ) {...}
if( booleanFunction() | otherBooleanFunction() ) {...}
So what's the difference between using | or using ||?
Note: I looked into this and found my own answer, which I included below. Please feel free to correct me or give your own view. There sure is room for improvement!
The two have different uses. Although in many cases (when dealing with booleans) it may appear that they have the same effect, it is important to note that the logical-OR is short circuit, meaning that if its first argument evaluates to true, then the second argument is left unevaluated. The bitwise operator evaluates both of its arguments regardless.
Similarly, the logical-AND is short-circuit, meaning that if its first argument evaluates to false, then the second is left unevaluated. Again, the bitwise-AND is not.
You can see this in action here:
int x = 0;
int y = 1;
System.out.println(x+y == 1 || y/x == 1);
System.out.println(x+y == 1 | y/x == 1);
The first print statement works just fine and returns true since the first argument evaluates to true, and hence evaluation stops. The second print statement errors since it is not short circuit, and a division by zero is encountered.
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:
The bitwise operator is not meant for that, which makes it harder to read but most importantly
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")){
//doNothing
}
System.out.println("Bitwise Operator:");
if(sayAndReturn(true, "first") | sayAndReturn(true, "second")){
//doNothing
}
}
public static boolean sayAndReturn(boolean ret, String msg){
System.out.println(msg);
return ret;
}
}
for programmers, there is only one difference.
your logical operators are logical ones,i.e. they test only one condition and get result based on that.
booleanFunction() || otherBooleanFunction() will be true if either is true. likewise, booleanFunction() && otherBooleanFunction() will be false if either is false. So, why test the other one. that's what logical operators do.
But bitwise ones check both. A frequent application of this concept is as follows.
if(someObject != null && someObject.somemethod())
So, in this case if you replace && by & then wait for a disaster. You will get nasty NullPointerException soon...
if( booleanFunction() || otherBooleanFunction() ) {...}
In this condition if booleanFunction() returns true then otherBooleanFunction() would not be executed.
if( booleanFunction() | otherBooleanFunction() ) {...}
But in bitwise operator both functions - booleanFunction() and otherBooleanFunction() would be executed no matter booleanFunction() returns true or false
Difference between bitwise a d logical operator-
1. Bitwise operator works on bit whereas logical operator works on statement.
2. Bitwise and is represented by & whereas logical and is represented by &&.
3. Bitwise or is represented by | whereas logical or is represented by ||.
I know that one of them is bitwise and the other is logical but I can not figure this out:
Scanner sc = new Scanner(System.in);
System.out.println("Enter ur integer");
int x=sc.nextInt();
if(x=0)//Error...it can not be converted from int to boolean
System.out.println("...");
The error means that x cannot be converted to boolean or the result of x=0 can not be converted to boolean.
== checks for equality.
= is assignment.
What you're doing is:
if( x = Blah ) - in Java this statement is illegal as you can not test the state of an assignment statement. Specifically, Java does not treat assignment as a boolean operation, which is required in an if statement. This is in contrast with C/C++, which DOES allow you to treat assignment as a boolean operation, and can be the result of many hair-pulling bugs.
When you write 'x = 0' you are saying "Store 0 in the variable x". The return value on the whole expression is '0' (it's like this so you can say silly things like x = y = 0).
When you write 'x == 0' it says "Does x equal 0?". The return value on this expression is going to be either 'true' or 'false'.
In Java, you can't just say if(0) because if expects a true/false answer. So putting if(x = 0) is not correct, but if(x == 0) is fine.
== is a comparison operator, and = is assignment.
== is an equality check. if (x == 0) // if x equals 0
= is an assignment. x = 0; // the value of x is now 0
I know the question has been answered, but this still comes up from time to time not as a programmer error but as a typographical error (i.e., the programmer knew what he meant, but failed). It can be hard to see, since the two look so similar.
I've found that a way to help avoid doing this is to put the constant expression on the left-hand-side, like so:
if (0 == x)
...
That way, if I accidentally use only one "=" sign, the compiler will fail with an error about assigning to a constant expression, whether or not the assignment operator is left-associative and whether the if conditional expects a strongly-typed Boolean.
if(x=0)
Here you're assigning the value of 0 to the variable x. The if statement in Java can't evaluate an integer argument as it can in many other languages. In Java, if requires a boolean. Try
if(x == 0)
to do a comparison.
Interpret the error to mean
"The expression
x=0
cannot be converted to Boolean."
Just to clarify about C/C++ - assignment is evaluated to the right operand
if(a = n)
is evaluated to n, so (n = 1) is true (n = 0) is false
One interesting note: Since assignment operator evaluates to the right operand, the following is valid in Java(albeit not pretty):
if (( x = blah ) > 0) ...
Parenthesis are needed because of operator precedence ( '>' binds stronger than '=').
As others have already said, '=' is assignment; '==' is compare.
in your program change
if(x=0)
to
if(x==0)
"==" checks for equality
"=" Is used for assignment.
It is giving you error cause you're assigning value to x in if(), where you're supposed to check for the equality. Try changing it to equality instead of assignment operator.
As others stated, = assigns while == compares.
However, these statements have their own values as well.
The = operator returns the value of its right-hand operand. This is how statements like a = b = c = 5 work: they are parsed as a = (b = (c = 5)), which evaluates to a = (b = 5) and then a = 5.
The == operator returns a boolean that is true if its operands are equal. The if statement runs its body if its argument is true. Thus, if headers like if (5 == 5) translate to if (true). This is why sometimes you see infinite while loops with header while (true); the while loop runs "while" toe argument is true.
If you had a boolean in your if statement, it would give no error and run the code if the value being assigned (or "compared to") was true. This is why it is so important to never mix up the = and == operators, especially when working with booleans.
Hope this helped!!