Hi,
why the compiler is not capable to understand that the String is null, even before it calls String.equals() ?
Thanks
About Question enthuware.ocpjp.v7.2.1223 :
Moderator: admin
-
- Posts: 27
- Joined: Sat Aug 31, 2013 5:38 pm
- Contact:
-
- Site Admin
- Posts: 10066
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1223 :
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.
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.
-
- Posts: 17
- Joined: Fri Oct 11, 2013 10:31 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1223 :
But if we have
then why still compiler compiles the code?
Code: Select all
final String str = null ;
-
- Site Admin
- Posts: 10066
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1223 :
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.
-
- Posts: 17
- Joined: Fri Oct 11, 2013 10:31 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1223 :
You are right , I mixed some other question. Thanks for your time.
Who is online
Users browsing this forum: No registered users and 258 guests