3
public class Test {
    public static void main(String... args) {

        int[][] arrayOfInts = {
            {23, 3, 65, 46},
            {65, 55, 2, 3},
            {55, 22, 35, 47}
        };
        int i, j, toFind = 2;

        boolean foundIt = false;

        search:
        for(i = 0; i < arrayOfInts.length; i++) {
            for(j = 0; j < arrayOfInts[i].length; j++) {
                if(arrayOfInts[i][j] == toFind) {
                    foundIt = true;
                    break search;
                }
            }
        }

        if(foundIt) 
            System.out.println("Element found at index " + i + ", " + j);
        else
            System.out.println("Element not found");
    }
}

Dear SO, I'm having trouble compiling the above code. My code works perfect when i initialize my integer variable j to 0 (j = 0).

But my question is Why should i initialize j = 0 ? Why am I getting variable j might not have been initialized error in the line

System.out.println("Element found at index " + i + ", "+ J);

If my int variable i is storing a value why cant j store.. ??

P.S. Noob here..!!

1
  • 1
    To expand on the other answers, arrayOfInts.length is calculated at runtime, not at compile time. In this very simple case a compiler could probably determine that arrayOfInts.length could not possibly be zero (we can see it's not zero just by reading the code) but in the more general case it cannot. Hence the compiler assumes that the inner for loop may never be executed.
    – jarmod
    May 31, 2013 at 11:20

7 Answers 7

2

Because imagine if you have arrayOfInts.length==0?

Then you never enter the loop --> j not initialized.

To prove my point try compiling and running with

int[][] arrayOfInts = {};
1
  • so try int i = 0, j = 0, toFind = 2; or, even better, use 3 lines for that. May 31, 2013 at 11:19
2

The reason is that, as far as the compiler is concerned, arrayOfInts.length could be 0 at runtime. If this happened, your for loop would never run - therefore, j would never have been initialised in the inner loop.

1

Suppose your outer for loop for(i = 0; i < arrayOfInts.length; i++) { never get chance to executes for some execution instance (as first time i < arrayOfInts.length false when length of array = 0) then j will remain uninitialized. And if foundIt be true, No value for j to print in if block hence error.

1

Since arrayOfInts.length could be 0, your loop may be never executed. In that case, your variable j will never be assigned a value.

1

It needs a lot of computation to check that you really put something in j before you use it in your println statement.

K, for example, wouldn't be initialized if arrayOfInts was of size 0.

That's why you have to initialize j, to let the compiler be sure it's initialized before you use it.

1

Because it is not a given that your inner loop, which initializes j will execute. Keep in mind that loop execution is conditional, so the conditions on your outer loop (the one with i) might not be true. In that case, the line that initializes j will never execute.

1

In java every local variable needs to be initialized.

On the first line of the for loop the variable i is getting initialized to 0. But the compiler is not sure if the second for loop will even get a chance to execute. Assume that

 arrayOfInts.length is 0. 

In this case the j value will not be initialized.

A nice thing would be to test it by changing the if(foundIt) to if(false), the compile will stop complaining.

EDIT Its all about, the compiler never assume things. It works on facts available and the fact there says the following: 1) i will always have a value. 2 j might not get a value and remain uninitialized. 3) foundIt can be true. :)

Moreover, its a really nice practice to initialize variables before using them.

The class level variables are automatically initialized to their default values at the class initialization time. But the method local variables are not, hence the complain/compile time error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.