0

There is this question that I am stuck on:

The answer is Version 3 but I am unsure why. I tried testing it with a test string assigned null, heres version 2 I used:

String str = null;
if (str != null || !str.equals("")){
    System.out.println(str);
}

And it was giving me a null pointer exception for all versions except version 3 which I am really confused about.

rhian
  • 23
  • 5
  • 1
    Welcome to Stack Overflow. "And it was giving me a null pointer exception for all versions except version 3 which I am really confused about." Well, do you know what a null pointer exception is? Do you know how `&&` and `||` work? Is it permissible to call a method on `null`? If the code has a method call on the *left* hand side of `&&` or `||`, will that call be attempted? Knowing those things, try to prove to me that the other versions of the code should work. (You cannot, because they do not; but the attempt should make it clear why they do not.) – Karl Knechtel May 02 '22 at 01:37
  • Please read https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ and try to talk through the code yourself; and read [ask] and https://meta.stackoverflow.com/questions/284236 and note that this is *not a discussion forum*. "I am confused about this" is not a question at all, let alone one appropriate for Stack Overflow. – Karl Knechtel May 02 '22 at 01:38
  • (If you *don't* know the answers to the questions in my first comment, you should review your textbook first, or talk to your instructor, or [try to find answers yourself](https://meta.stackoverflow.com/questions/261592), for example by [using a search engine](https://duckduckgo.com/?q=java+what+is+null+pointer+exception).) – Karl Knechtel May 02 '22 at 01:40
  • @jhanzaib said it all. these two questions could help also in your exercise https://stackoverflow.com/questions/2263660/in-java-what-are-the-boolean-order-of-operations and https://stackoverflow.com/questions/21202415/java-or-operator-precedence – hakima maarouf May 02 '22 at 01:40
  • As per the [*How to ask* guide](https://stackoverflow.com/help/how-to-ask), please [**do not** post images of code, data, error message, etc](https://meta.stackoverflow.com/questions/285551). Instead, copy or type the text into your question, [formatted as code](https://meta.stackoverflow.com/questions/251361/how-do-i-format-my-code-blocks/251362#251362). Reserve the use of images for diagrams or demonstrating rendering bugs; things that are impossible to describe accurately via text. – Bohemian May 02 '22 at 01:56

2 Answers2

2

If you do any operation on a null String, it gives null pointer exception. ( Here you are comparing it with !str.equals(""))

Another point to note is : in || condition , if first condition is true, the second wont be evaluated.

whereas in && , if the first condition is false , the second condition is not evaluated.

For this reason, Both of these are called short circuit operators.

Gurkirat Singh Guliani
  • 1,009
  • 1
  • 4
  • 19
1

The conditions in your if-statements have to be checked. So, if you have str != null || !str.equals("") in your if-statement, then you will check str != null and then check !str.equals("") if the first statement is false. Let's say that the string is null. The first condition is false and the second condition tries to check !str.equals(""). Then you get a NullPointerException, because you can't compare null to a string.

However, if you use str != null && !str.equals(""), then str != null will be checked first and the second statement !str.equals("") will only be checked if the first one is true. Meaning, the code will never get to !str.equals("") if the string is null. So, you never get the NullPointerException.

You can't use str.equals("") because null doesn't have an equals method. By using == you check the reference of your string. Everything that is null has the same reference, so if your string is null, then it will point to the null reference, and == can be used to check that refernce.

Jhanzaib Humayun
  • 1,193
  • 1
  • 4
  • 10
  • Great explanation thank you. Just to confirm a string can be checked against str != null but a null string cannot be checked against !str.equals("") ? – rhian May 02 '22 at 01:37
  • ". So, if you have str != null || !str.equals("") in your if-statement, then you will check str != null and then check !str.equals("") regardless of the outcome of the first condition since you are using ||, which means "or"" - this is wrong. ( if first condition is true, second wont be evaluated ) – Gurkirat Singh Guliani May 02 '22 at 01:38
  • 1
    Added an explanation for ` !str.equals("")`. @Gurkirat thanks for the correction. – Jhanzaib Humayun May 02 '22 at 01:52