1

When I type this code,

import java.util.*;

public class Example{
    public static void main(String args[]){
        Scanner input = new Scanner(System.in);
        System.out.print("Input an integer : ");
        int num = input.nextInt();
        int y;
        if(num>100){
            y=200;
        }
        if(num<100){
            y=200;
        }
        if(num==100){
            y=200;
        }
        System.out.println(y);
    }
}

There is an error saying "variable y might not have been initialized." Why?

Paul Kocian
  • 487
  • 4
  • 20
  • Does this answer your question? [Link](https://stackoverflow.com/questions/2448843/variable-might-not-have-been-initialized-error) – I AM GROOT Jan 06 '22 at 09:00
  • 2
    Because the compiler does not know that your `if` conditions cover all the possibilities. The compiler does not evaluate these statements. Just initialize y: `int y = 0;` – f1sh Jan 06 '22 at 09:01
  • you can, but you should have put a default value. for objects, this can be null, making it look like you didn't, but for primitives, you can't. you have to put a default value. – Stultuske Jan 06 '22 at 09:03
  • 2
    the compiler does not run the code (for all possibilities) to know that one of the `if` block will always be executed (compilation time would eventually *explode*) – user16320675 Jan 06 '22 at 09:18

3 Answers3

4

This error occurs when we are trying to use a local variable without initializing it. ( accessing it in the print statement) In your example, you have only written the if statement and compiler will not be able to detect it. To help the compiler, you can either use , the if else block which covers all the cases and it convinces the compiler or you can initialize it with value 0.

 int y;
    if(num>100){
        y=200;
    }
    else if(num<100){
        y=200;
    }
    else{
        y=200;
    }
    System.out.println(y);
Gurkirat Singh Guliani
  • 1,009
  • 1
  • 4
  • 19
2

In your code, you are declaring y as a "not initialized" variable int y;. At that point, y is not defined. If you change it to int y = 0; it will be fine.

The compiler knows that an if block may not be executed. So if the variable is only initialized inside the if block, it may not be initialized at all. And that is exactly the error message.

    Scanner input = new Scanner(System.in);
    System.out.print("Input an integer: ");
    final int LIMIT = 100; // less magic numbers
    int input = input.nextInt();
    int result = 0;

    if(input > LIMIT){
        result = 200;
    }
    if(input < LIMIT){
        result = 200;
    }
    if(input == LIMIT){
        result = 200;
    }
    System.out.println(result);

I modified your code a bit to solve the "not initialized" part. Also added a final variable for the 100 value since it is considered a good practice to name every value (no magic numbers).

You can also reduce code on this by changing the if statements a bit:

final int LIMIT = 100;
if (input == LIMIT || input < LIMIT || input > LIMIT) {
    result = 200;
}
Dockt
  • 31
  • 6
  • 1
    "The compiler does not know when y will be initialized" is not 100% correct, if you had used `if`-`else` the compiler would indeed know that the variable is initialized. The compiler just *knows* that an `if` block may not be executed. So if the variable is only initialized inside the `if` block, it may not be initialized (and that is exactly the error message) – user16320675 Jan 06 '22 at 09:24
  • @user16320675 Thanks for the clarification. I changed the answer. Based on your comment I did look it up and you are correct. – Dockt Jan 06 '22 at 12:06
1
import java.util.*;
class Example{
    public static void main(String args[]){
        Scanner input=new Scanner(System.in);
        System.out.print("Input an integer : ");
        int num=input.nextInt();
        int y=0;
        if(num>100){
            y=200;
        }
        if(num<100){
            y=200;
        }
        if(num==100){
            y=200;
        }
        System.out.println(y);
    }
}
Dudulu
  • 11
  • 2
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 06 '22 at 09:58