0

I just do not see why this isn't working:

public class HelloWorld {
    public static void main(String []args) {
        int a = 9;
        boolean isOdd;

        if (a != 0) {
            isOdd = false;
        } else if (a == 0) {
            isOdd = true;
        }

        if (isOdd == false) {
            System.out.print("Boolean is false"); 
        }
    }
}

HelloWorld.java:15: error: variable isOdd might not have been initialized

if (isOdd == false) {
    ^

Tom
  • 16,842
  • 17
  • 45
  • 54
Robert Tossly
  • 613
  • 1
  • 6
  • 14
  • Please don't repeat your question several times... the compiler message seems pretty clear, in this case. The compiler thinks `isOdd` may not be initialized - because you don't initialize it in every possible case that the *compiler* considers. – Jon Skeet Oct 26 '15 at 20:41
  • I think it is well explained here: http://stackoverflow.com/questions/24152351/java-error-variable-might-not-have-been-initialized – Andrea Oct 26 '15 at 20:43
  • First thing you should do when you get an error you are unfamiliar with is to do some research and see if you can find some info on your own. Fixing the problem this way not only helps you learn what the error means but also teaches you vital problem solving skills. Asking us to give you the answer is a quick and cheap way out - you will not learn as much. Asking a question on this site should be the last step you take, not the first. Just an FYI for next time. – Mage Xy Oct 26 '15 at 20:47
  • Most people prefer `if( ! isOdd ){...}` over comparing a boolean to `true` or `false`. – laune Oct 27 '15 at 06:13

6 Answers6

3

The compiler doesn't consider a != 0 and a == 0 to be mutually exclusive conditions - or rather, it doesn't look at whether two if conditions are mutually exclusive to work out what happens. So you can fix the error by changing your code to:

if(a != 0) {
  isOdd = false;
} else {
  isOdd = true;
}

At which point you'd be better off writing:

boolean isOdd = a == 0;

Which isn't a good test for oddness, but it would at least compile...

Basically, whenever you get a message like this, it means there's some way through your code that the compiler thinks might be plausible, but which doesn't initialize the variable. You've got:

if (condition1) {
    // Initialize variable
} else if (condition2) {
    // Initialize variable
}

So if neither condition1 nor condition2 is true, the variable won't be initialized. The only way in which the compiler really cares about the conditions here is if either evaluating the condition initializes the variable, or of the condition is a compile-time constant expression (in which case it can assume that that branch either is or isn't taken, depending on the value of that constant expression).

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
2
boolean isOdd;

        if(a != 0){
          isOdd = false;
        } else if(a == 0){
          isOdd = true;
        }

The above if statement has, strictly according to syntax, a third execution path, bypassing both "then" branches. Compilers don't use symbolic expression manipulation to determine that a!=0||a==0 evaluates to true, so that the third path is never taken and isOdd is assigned a value, no matter what. Thus, this statement should be

boolean isOdd;

        if(a != 0){
          isOdd = false;
        } else {
          isOdd = true;
        }
laune
  • 31,114
  • 3
  • 29
  • 42
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
1

Because

      boolean isOdd;
        if(a != 0){
          isOdd = false;
        } else if(a == 0){
          isOdd = true;
        }

has one flow of control where isOdd remains unassigned: The compiler does not reason that the conditions exclude the third path beyond the second else-if.

      boolean isOdd;
        if(a != 0){
          isOdd = false;
        } else {
          isOdd = true;
        }

Or best:

boolean isOdd = a == 0;
if( ! isOdd ) {
     System.out.print("isOdd is false"); 
}

Comparing a boolean to true or false is considered redundant by most.

laune
  • 31,114
  • 3
  • 29
  • 42
0

You need to initialize your primitive variable. Check this out for more info Java: Why am I required to initialize a primitive local variable?

 boolean isOdd = false;
Community
  • 1
  • 1
yogidilip
  • 790
  • 6
  • 21
0

Of course, if what you really care about is testing if a number is Odd or Even you could write:

boolean isOdd = a % 2 == 1;
  • You can of course adjust this to handle negative numbers by using `Math.abs()` such as: `boolean isOdd = Math.abs( a % 2 ) == 1;` –  Jan 20 '17 at 18:32
0

Others have answered why you are getting the error message. But the rest of your code is not correct, since it will say that 9 is not odd. To check if x is odd or even, you can use

   if ( (x & 1) == 1 ) {
      //odd
   } else {
      // even
   }
FredK
  • 4,094
  • 1
  • 9
  • 11