I’m having problem with this function in Java. I don’t understand why when lastNodeAttributes==null is true execution jumps to return null; as should, but right after that, instead of returning from function it jumps directly to return fight...; at the very end. Why first return doesn’t exit but execution jumps to return in second conditional part?? How is this even possible? Please explain cause obviously I don’t understand how basics of java work.
public Node undo() {
Node lastNode=fight.getLastChild();
NamedNodeMap lastNodeAttributes = lastNode.getAttributes();
if(lastNodeAttributes == null) { return null; }
else {
String lastNodeFighter = lastNodeAttributes.getNamedItem("fighter")
.getNodeValue();
String lastNodePoints = lastNodeAttributes.getNamedItem("points")
.getNodeValue();
if(Integer.parseInt(lastNodeFighter) == 1) {
fighter1score-=Integer.parseInt(lastNodePoints);
}
else { fighter2score -= Integer.parseInt(lastNodePoints); }
return fight.removeChild(fight.getLastChild());
}
}
What you’re describing is impossible. A return statement does indeed always return what is stated. There are a few possibilities to why you might see this behaviour in the debugger
Checking the actual return value will also confirm that indeed only one
returnstatement has executed. Do this by placing the breakpoint at the caller instead of the callee