Tuesday, 14 April 2015

Q. What will happen if final is used and final is removed in the following code.


public class Test

{

    public static void main(String[] args) throws Exception

    {

        final boolean x=true;

        while(x)

        {

            System.out.println("AA");

           

        }

        System.out.println(("BB"));

       

    }}


Ans:If final is used with x then will get C.E  saying Unreachable Code because the condition will always be true. If final is removed then there is a chance that the value of x may be changed (false) and so there is a chance of reaching the statement after while loop and so compilation is successful.


No comments:

Post a Comment