简体   繁体   中英

The order to check the conditions

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM