| Author |
How to differentiate between compile time errors and runtime errors ?
|
gurpeet singh
Ranch Hand
Joined: Apr 04, 2012
Posts: 895
|
|
Im having tough time in understanding questions related to compile time and runtime error. Can you please shed light on this topic as how to proceed in this regard? i have two code listings shown below:
Code Listing 1 :
Code Listing 2:
the compiler gives error in Code Listing 2 as to variable j might not have been initialized. Can you please tell me what the compiler thinks when it encounters code listing 1 and 2. And also the generalized version of the problem relating to compile time error and runtime exception. ??
|
OCPJP 6(100 %) OCEWCD 6(91 %)
|
 |
Prithvi Sehgal
Ranch Hand
Joined: Oct 13, 2009
Posts: 771
|
|
Dear Gurpreet,
Welcome to JR.
Well, there is a quick pointer. In code listing 1, the compiler will always execute the initialization for i before evaluating the condition.
So it compiles fine. In case-2, initialization of j is conditionally dependent on the outer loop. The compiler is not sure whether the outer
loop will even run or not, so it in such a scenario, it suspects as its dependent on outer loop, it is not necessary that j will initialize that's
why it will give the compile time error.
HTH,
|
Prithvi/Beenish,
My Blog, Follow me on Twitter,Scjp Tips, When you score low in mocks, Generics,Scjp Notes, JavaStudyGroup
|
 |
 |
|
|
subject: How to differentiate between compile time errors and runtime errors ?
|
|
|