0

I have this:

while(victim.getHealth() > 1 && !victim.getName().equals(this.getName())) {
    victim.hurt();
}

Now I noticed that in special occasions victim.getName() will return null (this is wanted behavior). But when that's the case I am expecting a NullPointerException to occur because we are basically calling null.equals(this.getName()) for the second condition.

However this doesn't happen. If I switch the order of the loop-conditions, e.g.

while(!victim.getName().equals(this.getName()) && victim.getHealth() > 1) {
    victim.hurt();
}

The NullPointerException will be thrown when getName() returns null.

Also: If this was || condition and not &&, in java the programm will stop checking for true condition once it found one right?

octavio
  • 456
  • 4
  • 14

1 Answers1

0

I didn't notice that in this special case where getName() == null victim.getHealth() will always be below 1 so java will never reach the second condition. Sorry I need a break now. :D

octavio
  • 456
  • 4
  • 14