This question already has answers here:
Java logical operator short-circuiting
(10 answers)
Boolean expressions optimizations in Java
(5 answers)
Closed 3 years ago.
I have a method with return statement like this:
return method(parameter 1, parameter 2) && method(parameter 2, parameter 1);\
However, when looking at my call tree, the second method is never being called (I see no calls with parameters like this). Can anyone explain why is this happening? Thanks
Possible short-circuiting, meaning if method(parameter 1, parameter 2) evaluates to false then the second method will never be called.
See also
Related
This question already has an answer here:
Purpose of Objects.isNull(...) / Objects.nonNull(...)
(1 answer)
Closed 12 months ago.
Can someone explain the following code?
if (Objects.nonNull(department.getDepartmentName()) && !"".equalsIgnoreCase(department.getDepartmentName())) {
depDB.setDepartmentName(department.getDepartmentName());
}
The first stab at understanding how a piece of code works should be look at the documentation available.
In your context, code intends to check if department.getDepartmentName() is both not-null and an empty string, only then set it to some value.
On an additional note, double check is redundant.
"".equalsIgnoreCase(department.getDepartmentName())
checks both, if department.getDepartmentName() is not null and is blank string.
This question already has answers here:
What is the Java ?: operator called and what does it do?
(17 answers)
Closed 4 years ago.
I am confused by this statement. Please help me explain this statement:
return third == Long.MIN_VALUE ? (int)first : (int)third;
Considering your top tags in SO is Python, let's explain this statement with Python:
return first if third == sys.maxint else third
Of course the Long.MIN_VALUE is not necessarily equal to sys.maxint in Python.
This question already has answers here:
Why can't I use the ternary ? operator to select between two function calls?
(4 answers)
Java: Ternary with no return. (For method calling)
(6 answers)
Closed 4 years ago.
c.getCollectibles()[i][j].isCollected() ? collectiblePanels[i][j].setSplash(c.getCollectibles()[i][j].getIcon()) : repaint();
I am writing a program in Java and have an error in my program. I am using a conditional operator. c.getCollectibles() returns a two-dimensional array of a Collectible class, and isCollected() is a non-static public method in the Collectible class that returns a boolean value.
collectiblePanels is a two-dimensional array of a class that contains a setSplash() method. The setSplash() method takes in the same type that getIcon() returns (the specifics are irrelevant to the error). Overall, setSplash() is void.
repaint() is a random void method. Again, specifics are irrelevant to the error.
The specific error is below:
The left-hand side of an assignment must be a variable
Syntax error on token "?", invalid AssignmentOperator
Syntax error on token ":", ; expected
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 7 years ago.
String TypedMaxNumber = MaxValue.getText();
if(TypedMaxNumber == "100")
System.out.println(TypedMaxNumber+" = 100");
I think it is a silly problem, but when I am running to run this program and I type 100 in the text Field it is not going inside the loop. What could be the reason.
I am running to run this program and I type 100 in the text Field it
is not going inside the loop.
Its
if(TypedMaxNumber.equals("100"))
Since TypedMaxNumber is of type String. equals() check for value equality
This question already has answers here:
Java logical operator short-circuiting
(10 answers)
Closed 8 years ago.
For instance if I have an if statement as follows:
if(returnsFalse() && timeConsumingFunction()){
//do whatever
}
Will the program run the time consuming function or will it realise that the if evaluates as false after the "returnsFalse()" function returns its value?
How does this work in different languages? Mainly interested in java and c.
No if you use && it will not continue on if the first statement is false.(Java) If you use & it will evaluate all expressions.