-2

I am trying to execute the below code:

public class HelloWorld{
     public static void main(String []args){
         int a=10,b=3,m;
         System.out.println("Hello World "+a+" " + b);
     }
}

I was expecting a compilation error for not initializing local variable 'm' but the program ran successfully and gave me the output.

Why is it so ? I was thinking in all the cases if the local variable is not initialized the compiler will give an error.

When I try to compile the code given below

public class HelloWorld{
     public static void main(String []args){
         int a=10,b=3,m;
         System.out.println("Hello World "+a+" " + b + " " +m);
     }
}

Here I am using the value of 'm' and I am getting error for not initializing local variable.

But why doesn't Java show an error in the first case?

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Alex
  • 33
  • 1
  • 6
  • I'm pretty sure the JVM doesn't care about this stuff, ergo, there is no reason for the JVM to throw any errors here. – Jörg W Mittag May 19 '19 at 04:25
  • Usage: The *compiler* *reports* compile time errors. They are not thrown (exceptions are) and the JVM doesn’t deal with them. – Ole V.V. May 19 '19 at 05:29
  • I took . the freedom to reword your question to be in line with java terminology. You can undo my changes if you think they do not reflect your intention! – GhostCat May 19 '19 at 05:44
  • 1
    Possible duplicate of [Why must local variables, including primitives, always be initialized in Java?](https://stackoverflow.com/questions/1560685/why-must-local-variables-including-primitives-always-be-initialized-in-java) – Andronicus May 19 '19 at 09:53

2 Answers2

2

As far as the Java compiler is concerned, uninitialized local variables are fine. As long as you don’t try to use their value, that is. Only your IDE should warn you that m is not used. My Eclipse says “The value of the local variable m is not used”. It’s just a warning, nothing that stops your program from running.

It’s when you try to use the (undefined) value, as in your second snippet, that the error is reported.

Consider this snippet. I am reusing your two lines and have added two more.

        int a=10,b=3,m;
        System.out.println("Hello World "+a+" " + b);
        m = 14;
        System.out.println(m);

It’s running fine too. I include it to illustrate that when the compiler reads your two lines, it doesn’t check whether m is initialized later and therefore finds no reason for objection so far. (As an aside, better code style would be to declare m only when it is initilized, and also declare each variable on a separate line.)

PS Whether you declare m on its own line or together with a and b is of no concern to the compiler, it’s a purely stylistic question.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
1

There is a compiler error for using an uninitialized local variable. Only then it is necessary that it a variable has a defined value.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
Sean F
  • 4,344
  • 16
  • 30