How does AND and OR statements work inside an IF condition

0 votes
I have the following code:

if(!partialHits.get(req_nr).containsKey(z) || partialHits.get(req_nr).get(z) < tmpmap.get(z)){  
    partialHits.get(z).put(z, tmpmap.get(z));  
}

partialHits is a HashMap. 
Will JAVA check the second statement if first statement is True ? 
For the first statement to be true, the HashMap should not contain the given key therefore if the second statement is checked, we get NullPointerException.
Other way round, consider this condition in JAVA

if(a && b)  
if(a || b)

How does this work ?
May 3, 2018 in Java by developer_1
• 3,350 points
1,021 views

2 answers to this question.

0 votes
No, second statement won't be evaluated. This proves to be very useful. For eg, if you need to test whether a String is not null or empty, you can write:

if (str != null && !str.isEmpty()) {
  doSomethingWith(str.charAt(0));
}

or, the other way around

if (str == null || str.isEmpty()) {
  complainAboutUnusableString();
} else {
  doSomethingWith(str.charAt(0));
}

If we didn't have 'short-circuits' in Java, we'd receive a lot of NullPointerExceptions in the above lines of code.
answered May 3, 2018 by Rishabh
• 3,620 points
0 votes

Short circuit here means that the second condition won't be evaluated.

If ( A && B ) will result in short circuit if A is False.

If ( A && B ) will not result in short Circuit if A is True.

If ( A || B ) will result in short circuit if A is True.

If ( A || B ) will not result in short circuit if A is False.

answered Sep 19, 2018 by Sushmita
• 6,920 points

Related Questions In Java

0 votes
0 answers

How does the "this" keyword work, and when should it be used?

I'm trying to figure out what the&nbs ...READ MORE

Dec 7, 2022 in Java by Nicholas
• 7,760 points
624 views
0 votes
1 answer

How does contains() method work in evaluating objects?

Generally you should also override hashCode() each time you ...READ MORE

answered May 25, 2018 in Java by Rishabh
• 3,620 points
1,015 views
0 votes
1 answer

How to getters and setters work in java

In Java, getter and setter are two ...READ MORE

answered Jun 1, 2018 in Java by Daisy
• 8,140 points
1,724 views
0 votes
1 answer

How can I get the current date and time in UTC or GMT in Java?

This definitely returns UTC time: as String ...READ MORE

answered Jun 7, 2018 in Java by Rishabh
• 3,620 points
27,613 views
0 votes
1 answer

Perform "Switch-case" on String in Java

Switches based on integers can be optimized ...READ MORE

answered May 8, 2018 in Java by Rishabh
• 3,620 points
952 views
+5 votes
4 answers

How to execute a python file with few arguments in java?

You can use Java Runtime.exec() to run python script, ...READ MORE

answered Mar 27, 2018 in Java by DragonLord999
• 8,450 points

edited Nov 7, 2018 by Omkar 81,222 views
+1 vote
1 answer

How to handle drop downs using Selenium WebDriver in Java

First, find an XPath which will return ...READ MORE

answered Mar 27, 2018 in Selenium by nsv999
• 5,500 points
8,357 views
0 votes
1 answer

What are the differences between getText() and getAttribute() functions in Selenium WebDriver?

See, both are used to retrieve something ...READ MORE

answered Apr 5, 2018 in Selenium by nsv999
• 5,500 points
17,432 views
0 votes
2 answers

How do I read and convert an InputStream object to string type?

You can also use Java Standard Library ...READ MORE

answered Jul 17, 2018 in Java by Sushmita
• 6,920 points
17,227 views
0 votes
1 answer

How to read and write on an excel files?

Follow these steps: Write: public class User { ...READ MORE

answered May 15, 2018 in Java by Rishabh
• 3,620 points
1,539 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP