What will happen when you try to compile and run the following code? It is assumed that the outstream file is already created.

import java.io.*;

public class Main {
    public static void main(String[] args) {
        try {
            FileInputStream fis = new FileInputStream("outstream");
            InputStreamReader isr = new InputStreamReader(fis);
            BufferedReader br = new BufferedReader(isr);
            try {
                do {
                    String str = br.readLine();
                } while (str != null);             
            finally {
        } catch(IOException e){
Compilation error will occur due to the fact that the str variable is declared inside the do .. while() loop.
Of course, it is allowed to declare variables inside loops. But then they should be used inside the loop as well. In the given example the str variable is used outside of the block in which it was declared - in a loop continuation.

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback