This question already has answers here:
Why isn't the right side of && operator evaluated? [duplicate]
(4 answers)
Closed 3 years ago.
Could someone tell me why the value of a is not changing in the following code?
int a = 10;
System.out.print( (a < 5) && (++a==11));
System.out.println(a);
You're seeing "short circuit evaluation" in action. The second part of the boolean expression (++a==11) is never evaluated, because (a < 5) is false. In this case, the JVM knows that the whole expression is false before it evaluates (++a==11), so it skips it entirely.
This is also a great example of why such "side effects" are bad in logical tests: you're mutating the values you're evaluating in an unpredictable way. In a non-trivial program, you don't necessarily know whether (a < 5) is true, so it's difficult to know whether a will be incremented or not.
Related
This question already has answers here:
Why is order of expressions in if statement important
(9 answers)
short-circuiting behavior of conditional OR operator(||)
(3 answers)
Closed 3 years ago.
I have stumbled upon this odd behaviour while working with jasper soft reports and I would like to have it explained since I don't know much about Java.
Basically, I have a print when expression:
($P{Parameter_name}!=0 || $P{Parameter_name}==null)
? true : false
-> this returns false
($P{Parameter_name}==null || $P{Parameter_name}!=0)
? true : false
-> this returns true
The only difference is the equation order but logically it shouldn't make a difference.
The parameter is null.
I know that for example SQL wouldn't care for the order and would always evaluate true with a simple or statement like this.
If the parameter is null, you will get a NullPointerException when evalulating
$P{Parameter_name}!=0
And maybe this exception will prevent Jasper from checking the second condition.
In your second version, the test
$P{Parameter_name} == null
will be evaluated to true, which means that
$P{Parameter_name} != 0
won't be evaluated at all (and no NPE will be raised)
This question already has answers here:
Compare one String with multiple values in one expression
(20 answers)
String.equals() with multiple conditions (and one action on result) [duplicate]
(7 answers)
Closed 4 years ago.
Need help with if else statement in Java. Need the program to output when either l.getPlot().equals("MR") or ("X") and if l.getZone().equals("UP SPEC") set the top upper limit.
Can anyone explain to me how to properly set that up so when the query is a match for MR or X it will set the top upper.
Note: If I remove || ("X") it works for all the MR items but leaves all the ("X") blank.
if (l.getPlot().equals("MR")) || ("X"){
if (l.getZone().equals("UP SPEC")) {
limit.setTopUpper(l.getLimit());
} else if (l.getZone().equals("LO SPEC")) {
limit.setTopLower(l.getLimit());
}
}
This should do the trick because it applies the logical "or" to two conditions, instead of one condition and one bare string.
if (l.getPlot().equals("MR")) || (l.getPlot().equals("X")){
Here is an improved version (credits Tim Biegeleisen), which avoids a null pointer exception.
if ("MR".equals(l.getPlot()) || "X".equals(l.getPlot()))
If the argument to equals() evaluates to NULL, the result is a clean false.
This question already has answers here:
&& (AND) and || (OR) in IF statements
(9 answers)
Closed 6 years ago.
I have a doubt of how this if loop is going to work. if fn1 evaluates to be true, will it still go for checking fn2 or will it go into the if loop and add the elements into the list?
The || operator is short-circuited, meaning that if the left-hand operand is true (whatever that means in the language in question), the right-hand operator isn't evaluated at all. So in your example, fn1 will definitely be called, but fn2 will only be called if fn1 returns false.
This question already has answers here:
What is the Java ?: operator called and what does it do?
(17 answers)
Closed 8 years ago.
I came across a code snippet online that used a notation that from what I gather seems to do a comparison and then returns back possible multiple outputs. I am still confused about it, even after research. Can someone re-write the code snippet to an equivalent, more basic version so that I can make sure I am understanding what I am seeing?
int mPart = i < mParts.length ? Integer.parseInt(mParts[i]) : 0;
Thanks in advance!
This is ternary IF operator. This line is equal to
int mPart;
if(i < mParts.length) {
mPart = Integer.parseInt(mParts[i]);
} else {
mPart = 0;
}
This question already has answers here:
why Integer.MAX_VALUE + 1 == Integer.MIN_VALUE?
(9 answers)
Closed 8 years ago.
After going through this question, i had one another in my mind.
Question: Why an integer variable value is set to Integer.MAX_VALUE.
eg.
int x = Integer.MIN_VALUE;
x--;
if (x == Integer.MAX_VALUE) {
System.out.println("Why....");
}
There must be some reason that is why this behavior implemented explicitly, otherwise throwing an Exception would be a better idea. I could not find/locate this behavior in JLS.
Because of underflow. Computers have worked like this for years, throwing an Exception would be a horrible idea here.
http://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.2
"The integer operators do not indicate overflow or underflow in any way."