Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When do I need to initialize a variable in java?

Tags:

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.

like image 883
Expodecay Avatar asked Jun 07 '26 18:06

Expodecay


1 Answers

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.

like image 56
Thilo Avatar answered Jun 10 '26 07:06

Thilo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!