Expodecay
Expodecay

Reputation: 33

When do I need to initialize a variable in java?

I have been trying to learn Java and have hit a roadblock with variable initialization. In both examples, the program pulls input from the keyboard, sorts the values in ascending order, then prints the results. Both programs compile and function properly, but one needs the variables to be initialized while the other does not.

In this example, I didn't initialize anything, and the program compiled with no errors.

    int num1, num2, num3, temp;      // DIDN'T INITIALIZE

    Scanner input = new Scanner(System.in);

    System.out.print("Enter an integer: ");
    num1 = input.nextInt();

    System.out.print("Enter an integer");
    num2 = input.nextInt();

    System.out.print("Enter an integer: ");
    num3 = input.nextInt();

    if (num1 > num2) {temp = num1; num1 = num2; num2 = temp;}
    if (num2 > num3) {temp = num2; num2 = num3; num3 = temp;}

    if (num1 > num2) {temp = num1;num1 = num2; num2 = temp;}

    System.out.println(num1 + " " + num2 + " " + num3 );

However, in this example, if I don't initialize the variables from the beginning, I get an error that reads "java variables might not have been initialized."

    int num1=0 , num2=0, num3=0, temp, i=0;  // MUST INITIALIZE VARIABLES!!

    Scanner input = new Scanner(System.in);
    while (i < 3) {
        System.out.print("Enter an integer: ");
        while(true){
            while (!input.hasNextInt()) {
                System.out.print("Not an integer!! Please enter an integer: ");
                input.next();
            }
            if (i < 1) {num1 = input.nextInt();break;}
            if (i < 2) {num2 = input.nextInt();break;}
            if (i < 3) {num3 = input.nextInt();break;}
        }
        i++;
    }

    if (num1 > num2) {temp = num1; num1 = num2; num2 = temp;}
    if (num2 > num3) {temp = num2; num2 = num3; num3 = temp;}

    if (num1 > num2) {temp = num1;num1 = num2; num2 = temp;}

    System.out.println(num1 + " " + num2 + " " + num3 );

The only difference between these programs is that the second uses while loops to check the users input, but aside from that, the method of assigning values, and way they are sorted are identical.

Upvotes: 3

Views: 387

Answers (3)

Thomas Shields
Thomas Shields

Reputation: 8942

The compiler can't tell that the code that initializes the variables actually executes. The initialization (num1 = input.nextInt()) happens inside of an if block inside a while block. While you can tell that that code will always execute, the compiler doesn't look that far ahead.

This is why it's considered best practice to always initialize any variables to their default value (in this case, 0 for integers)

Upvotes: 1

Thilo
Thilo

Reputation: 262474

The compiler makes sure that all variables have been assigned to before you read from them (if you never read from them, or the first thing you do with them is write to them then they don't need to be initialized at definition).

In the second examples, the variables are only initialized when certain conditions are met (they are hidden between if and while blocks).

Now, you may know that this will always be the case, but the compiler does not reason that far, so it will error out because it cannot ensure that the variables have always been initialized after the loop.

Upvotes: 3

Abdelhak
Abdelhak

Reputation: 8387

You need to initialize the variables in java when these variables not sure to have the default values

Upvotes: 0

Related Questions