If 'number' is null, then won't number.length() == 10 throw a NullPointerException? Which would be another reason why this code will not work in all situations if assertions are enabled? And would that make the code syntactically incorrect? Or does this have nothing to do with syntactical correctness?
public void processPhoneNumber(String number) {
assert number != null && number.length() == 10 : "Invalid phone number"; ... }
Thanks
About Question enthuware.ocpjp.v8.2.1399 :
Moderator: admin
-
- Posts: 32
- Joined: Tue Apr 12, 2016 9:19 pm
- Contact:
-
- Site Admin
- Posts: 10388
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1399 :
Since && is a short circuiting operator, if number is null, number.length() part of the expression will not be executed.
-
- Posts: 32
- Joined: Tue Apr 12, 2016 9:19 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1399 :
Thanks for replying. But is that syntactically correct? I'm guessing you think it is.
-
- Site Admin
- Posts: 10388
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1399 :
Yes, it is syntactically correct. No compilation error.
You can actually try it out with a simple test program.
You can actually try it out with a simple test program.
Who is online
Users browsing this forum: No registered users and 3 guests