Home > Software design >  Do i have to initialise a variable in every if else statement in Java?
Do i have to initialise a variable in every if else statement in Java?

Time:02-06

    int outsideTem = 10;
    String output;
    if(outsideTem < 0){
        //output = "Grab a coat";// i get an error if i comment out this line but why?
        //System.out.println(output);
    }
    else if(outsideTem < 15){
        output = "Grab a cardigan";
        //System.out.println(output);
    }
    else{
        output = "HOT!!!";
        //System.out.println("HOT!!!");
    }
    System.out.println(output);

Getting an error if i comment out the variable from the if block. But I tried to intialise it before and it is working. But I am not sure why

    int outsideTem = 10;
    String output = "";// tried this and it is working but not sure why
    if(outsideTem < 0){
        //output = "Grab a coat";// i get an error if i comment out this line but why?
        //System.out.println(output);
    }
    else if(outsideTem < 15){
        output = "Grab a cardigan";
        //System.out.println(output);
    }
    else{
        output = "HOT!!!";
        //System.out.println("HOT!!!");
    }
    System.out.println(output);

CodePudding user response:

Yes, variables need to be initialized before you can use them. You are trying to use the variable in the print statement. In case outsideTemp is smaller than 0 and the line is commented out, what would you expect Java to print in that line?

You don't need to initialize in the if however. You could already initialize it when you declare the variable.

So the following would work:

public class Application {

    public static void main(String[] args) {
        int outsideTem = 10;
        // initialize here
        String output = "";
        if(outsideTem < 0){ // unused if here, but compiling code
            //output = "Grab a coat";
            //System.out.println(output);
        }
        else if(outsideTem < 15){
            output = "Grab a cardigan";
            //System.out.println(output);
        }
        else{
            output = "HOT!!!";
            //System.out.println("HOT!!!");
        }
        System.out.println(output);
    }

}

CodePudding user response:

Do I have to initialise a variable in every if else statement in Java?

Yes! But only for local variables. A local variable must be assigned a value before it can be referenced. But that assignment could take place within the same statement in which it us referenced. Read Definite Assignment in the Java Language Specification . Here are some excerpts.

Example 16-1. Definite Assignment Considers Structure of Statements and Expressions

A Java compiler recognizes that k is definitely assigned before its access (as an argument of a method invocation) in the code:

{
    int k;
    if (v > 0 && (k = System.in.read()) >= 0)
        System.out.println(k);
}

because the access occurs only if the value of the expression:

v > 0 && (k = System.in.read()) >= 0

is true, and the value can be true only if the assignment to k is executed (more properly, evaluated).

Similarly, a Java compiler will recognize that in the code:


{
    int k;
    while (true) {
        k = n;
        if (k >= 5) break;
        n = 6;
    }
    System.out.println(k);
}

the variable k is definitely assigned by the while statement because the condition expression true never has the value false, so only the break statement can cause the while statement to complete normally, and k is definitely assigned before the break statement.

{
    int k;
    while (n < 4) {
        k = n;
        if (k >= 5) break;
        n = 6;
    }
    System.out.println(k);  /* k is not "definitely assigned"
                               before this statement */
}

must be rejected by a Java compiler, because in this case the while statement is not guaranteed to execute its body as far as the rules of definite assignment are concerned.

  • Related