0

All code is in Java.

public class TestLocal 
{
    public static void main(String [] args) 
    {
        int x;
         if (args[0] != null) 
         { // assume you know this will
           // always be true
             x = 7;  // statement will run
         }
     int y = x; // the compiler will choke here
    }
}

So, my Question is why does the compiler choke here? Does it bypass the if statement (This look quite horrible...) If I initialize x outside the if statement block then the compiler does not complain, as in this code:

public class TestLocal 
{
    public static void main(String [] args) 
    {
        int x;
        x = 7; // compiler is happy now..:)
        if (args[0] != null) 
        { // assume you know this will
         // always be true
         // statement will run
        }
        int y = x; // the compiler not complain here now.
    }
}

Why such a horrible behavior from the compiler?

ElliotSchmelliot
  • 7,322
  • 4
  • 41
  • 64
  • Because it is not certain that program gets into if statement.If you want to do a x=7 statement just do it but, give an initial value on the upper side to the x. – ihsan kocak Aug 08 '13 at 06:59
  • `// assume you know this will always be true statement will run` Then why not take everything in that `if` statement out and instead `assert args[0] != null` right after `x = 7`? – Dennis Meng Aug 08 '13 at 07:02
  • Where did you learn Java? Who said it is "true". – Ajay Bhojak Aug 08 '13 at 07:26

7 Answers7

5

Java requires you to initialize the local variable as a safety measure. It stops you from accidently reading an odd value. If the compiler didn't complain and you were to read the value of x, it could be anything.

RaptorDotCpp
  • 1,425
  • 14
  • 26
  • 3
    The compiler can't know that that if-statement is always going to be true. If it isn't true, x is still not initialized. – RaptorDotCpp Aug 08 '13 at 07:01
2

Well, the thing is that args[0] might be null, and then x would not be initialized before use. That is why the compiler is complaining at you.

Adriaan Stander
  • 162,879
  • 31
  • 289
  • 284
2

When you don't pass argument then String[] array has nothing and is uninitialized. so the if condition will not be true in this case and even it will throw Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0.. Now assume you have set args[0] to null value at some place in your code then the if(args[0]) != null) will evaluate to false . In that case you are assigning int y= x; where x is uninitialized. And I think you should know compiler takes these things into account. In java you can not use a local variable until it is initialized.

That's why the smart Java compiler gives you compile time error and tell you code properly.

:-). Hope you got it.

Ajay Bhojak
  • 1,161
  • 9
  • 17
1

According to java Local variables must be initialized before use.
in your case you are trying to initialize it in condition.Conditions never guaranteed to executes(In run time it may executes or may not execute based on the condition) and trying to assign to y variable.
So compiler warns about it.

Prabhaker A
  • 8,317
  • 1
  • 18
  • 24
0

Because of performance issues among others, Java doesn't initialize automatically local variables. so here:

 int x;

x still have an undefined value. Compiler won't allow you to compile the code because you are referring to x in this line:

int y = x;

It works in the second case because you are initializing x before it is referred elsewehere.

Granted, you are initializing x inside the if statement, but obviously it could be the case that your if evaluates to false, so the compiler takes that into account.

morgano
  • 17,210
  • 10
  • 45
  • 56
0

int y = x; // the compiler will choke here

Because in case your if block does not execute(when arg[0] ==null) then x will not be initialized. Java compiler mandates the variable to be initialized before they can be used.

Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
-1

You're declaring x as space to hold an integer, but since you didn't give it any integer to hold, it is still null.

The compiler looks at both the true and false paths of your if statement. If arg[0] contains something, then you're fine. Otherwise, when you try to set y = x, x has nothing to give to y and you get an error.

Xiode
  • 1