-1

I have two different cases where I have used boolean in if condition. Why I need to initialize variable p in CASE 1?

CASE 1:

public static void main(String[] args) {
    int p;
    if(Boolean.TRUE){
        p=100;
    }
    System.out.println(p);
}

CASE 2:

public static void main(String[] args) {
    int p;
    if(Boolean.TRUE){
        p=100;
    }
    System.out.println(p);
}
Balkrushn
  • 91
  • 1
  • 1
  • 12

3 Answers3

1

Simple answer from Oracle:

Local variables are slightly different; the compiler never assigns a default value to an uninitialized local variable. If you cannot initialize your local variable where it is declared, make sure to assign it a value before you attempt to use it. Accessing an uninitialized local variable will result in a compile-time error.

And although p is always initialized from the if statement, but the compiler will investigate all the cases of a wrapper Boolean. To solve it:

public static void main(String[] args) {
   int p;
   if(Boolean.TRUE){
       p=100;
   } else {
       p= 0; //for example. The compiler will see all the cases are covered
   }
   System.out.println(p);
}//no error
T D Nguyen
  • 7,054
  • 4
  • 51
  • 71
0

The compiler complains because it thinks there is a path in which p could be used in the System.out.println without ever having been assigned to anything. It sees the if statement, and decides that since p is assigned only when the expression is true, but not when it's false, it's possible that p might be unassigned after that.

Of course we look at this and figure the compiler has to be really dumb, because it's impossible for the expression to be false. Well ... in order to be consistent and not have behavior that varies from a compiler written by one vendor to a compiler written by another, the rules have to be specific about what the compiler is and isn't allowed to "figure out" about expressions. And apparently the rules don't let it figure out that Boolean.TRUE is always true. However, I haven't memorized all the rules about definite assignment, so I don't know particularly what rules apply in this case.

More: The duplicate question mentioned in @Ravi Jiyani's comment does a good job of explaining the rules that I didn't know about.

ajb
  • 31,309
  • 3
  • 58
  • 84
0

Boolean.TRUE is a wrapper object and singleton. that's why it throws an error because it does not know what hides behind Boolean.TRUE.

Therefore the variable p is not guaranteed to be initialized before it prints.

try to change the condition to if(true) and it will compile with no error because if (true) the compiler can deduce that phas been initialized before it's being read

Dyrandz Famador
  • 4,499
  • 5
  • 25
  • 40
  • 1
    This isn't quite accurate. The compiler knows very well that a `true` is hiding behind `Boolean.TRUE`, since the language defines it that way and it's `final` so it can't be changed. It's just that the language rules put limits on what the compiler is allowed to deduce in this situation. – ajb Feb 18 '15 at 05:01