1

So i have

if(A != null && (A.getFullName() == null || A.getFirstName().equals("")){}

Does the jvm check A.getFullName first before checking A.getFirstName? Is the order from left to right or could it check A.getFirstName before A.getFullName? I am asking because if A.getFullName is null, A.getFirstName will give me a NullPointerException

  • Are you Trying to compare String values? – Kebab Programmer Feb 11 '15 at 20:10
  • Java strings are double quotes and you're missing a parenthesis. – Malik Brahimi Feb 11 '15 at 20:11
  • Object like string need .equals(object). Also you will get a runtime exeption as JVM doesn't process it as you say left to right you need to check beforehand. – Ya Wang Feb 11 '15 at 20:11
  • 1
    Well, yes, the left hand operand of `||` is always evaluated before the right hand operand; moreover the right hand operator won't be evaluated if the left hand operand comes out true. But you need some parentheses to call a method - like `getFullName()`, and you need to use `equals()` to compare strings, not `==`. – Dawood ibn Kareem Feb 11 '15 at 20:11
  • Sorry, yes I am comparing with .equals. Edited the OP – JohnnyGnomez Feb 11 '15 at 20:12

1 Answers1

3

Yes, Java's logical operator do short-circuit. In your case this means that if A.getFullName() returns null, A.getFirstName() will not be called.

See Java logical operator short-circuiting for further discussion.

Community
  • 1
  • 1
NPE
  • 486,780
  • 108
  • 951
  • 1,012