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)
Related
This question already has answers here:
Ternary operator gives "Not a statement" error [duplicate]
(2 answers)
Java Ternary without Assignment
(4 answers)
Closed 1 year ago.
I am trying to make a simple ternary operation on a hash map:
hashMap.get(number) == 1 ?
hashMap.remove(number) :
hashMap.merge(number, 1, Math::subtractExact);
I am getting a 'Not a statement' error from my IDE. What I don't understand is where this is coming from: both calls to remove and merge sound like statements to me.
You have to assign the value of the ternary operator to some variable:
Integer value =
hashMap.get(number) == 1 ?
hashMap.remove(number) :
hashMap.merge(number, 1, Math::subtractExact);
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.
This question already has answers here:
StringFormat for Java Boolean Operator
(6 answers)
Closed 4 years ago.
I'm sure that has been asked before, but I couldn't find the answer.
Boolean nullValue = null;
Boolean falseValue = false;
System.out.println( String.format( "%b %b", nullValue, falseValue ) );
How is the output of this "false false"? That's exactly what I get out on my machine. Java 8. Mac. Java noob.
EDIT
So this is the intended behavior. It doesn't look like Boolean toString produces my desired behavior of outputting null, false, true either. Is everyone who wants this supposed to write a method on their own? Is there function that produces that output?
If you look in the method java.util.Formatter.FormatSpecifier.printBoolean(Object) it only prints "true" if the variable is true - otherwise false is printed.
Looking further this has been covered in a previous StackOverflow question. This behavior is specified in the documentation.
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.