Page 1 of 1
About Question enthuware.ocpjp.v7.2.1223 :
Posted: Fri Sep 13, 2013 4:44 pm
by sinapse
Hi,
why the compiler is not capable to understand that the String is null, even before it calls String.equals() ?
Thanks
Re: About Question enthuware.ocpjp.v7.2.1223 :
Posted: Fri Sep 13, 2013 8:36 pm
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.
Re: About Question enthuware.ocpjp.v7.2.1223 :
Posted: Mon Sep 01, 2014 7:00 am
by hamada.yamasaki
But if we have
then why still compiler compiles the code?
Re: About Question enthuware.ocpjp.v7.2.1223 :
Posted: Mon Sep 01, 2014 8:37 am
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.
Re: About Question enthuware.ocpjp.v7.2.1223 :
Posted: Mon Sep 01, 2014 9:42 am
by hamada.yamasaki
You are right , I mixed some other question. Thanks for your time.