About Question enthuware.ocpjp.v7.2.1223 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
sinapse
Posts: 27
Joined: Sat Aug 31, 2013 5:38 pm
Contact:

About Question enthuware.ocpjp.v7.2.1223 :

Post by sinapse »

Hi,
why the compiler is not capable to understand that the String is null, even before it calls String.equals() ?

Thanks

admin
Site Admin
Posts: 10066
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1223 :

Post by admin »

1. Compiler takes only the values of compile time constants into consideration while generating errors. In this case, str is a variable and so it ignores it value.
2. Compiler doesn't execute any code so it doesn't really "know" the value of a variable.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

hamada.yamasaki
Posts: 17
Joined: Fri Oct 11, 2013 10:31 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1223 :

Post by hamada.yamasaki »

But if we have

Code: Select all

 final String str = null ;
then why still compiler compiles the code?

admin
Site Admin
Posts: 10066
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1223 :

Post by admin »

Hamada, the compiler knows that str is null in your example but I am not sure why you think that it should not compile. null check happens at run time. So there is no reason for a compilation error here.
If you like our products and services, please help us by posting your review here.

hamada.yamasaki
Posts: 17
Joined: Fri Oct 11, 2013 10:31 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1223 :

Post by hamada.yamasaki »

You are right , I mixed some other question. Thanks for your time.

Post Reply

Who is online

Users browsing this forum: No registered users and 258 guests