Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why must some variables be initialized with a value (e.g. 0, null)?

Tags:

java

null

Why do some strings (e.g. topStudent1) have to be set to null, while others (e.g. name1) do not, in order to avoid a compiler error? Why do some doubles (e.g. highScore) have to be set to 0, while others (e.g. score1) do not, in order to avoid a compiler error?

public class Exercise05_09 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter the number of students: ");
        int numOfStudents = input.nextInt();

        String name1;
        String name2;
        String topStudent1 = null;
        String topStudent2 = null;
        double score1;
        double score2;
        double highScore = 0;
        double nextHighScore = 0;
        int count = 2;

    while (count <= numOfStudents) {
        if (count == 2) {
        System.out.println("Enter a student name: ");
        name1 = input.next();
        System.out.println("Enter a student score: ");
        score1 = input.nextDouble();
        System.out.println("Enter a student name: ");
        name2 = input.next();
        System.out.println("Enter a student score: ");
        score2 = input.nextDouble();
                if (score1 > score2) {
                    highScore = score1;
                    topStudent1 = name1;
                    nextHighScore = score2;
                    topStudent2 = name2;
                }
                else {
                    highScore = score2;
                    topStudent1 = name2;
                    nextHighScore = score1;
                    topStudent2 = name1;
                }
        }
        else {
            System.out.println("Enter a student name: ");
            name1 = input.next();
            System.out.println("Enter a student score: ");
            score1 = input.nextDouble();
                if (score1 > highScore) {
                    nextHighScore = highScore;
                    highScore = score1;
                    topStudent2 = topStudent1;
                    topStudent1 = name1;
                }
                else if (score1 > nextHighScore) {
                    nextHighScore = score1;
                    topStudent2 = name1;
                }
        }
       count++;    
    }
    System.out.printf("Top two students:\n%s's score is %3.1f\n%s's score is %3.1f\n", topStudent1, highScore, topStudent2, nextHighScore);
     }
}

Thanks!!

like image 289
so8857 Avatar asked Dec 19 '22 19:12

so8857


1 Answers

All variables must be definitely assigned (JLS §16) before they are accessed, and the compiler verifies this:

Each local variable (§14.4) and every blank final field (§4.12.4, §8.3.1.2) must have a definitely assigned value when any access of its value occurs.

Since the while loop may not execute at all (e.g. user enters 1), the use of the 4 variables (e.g. topStudent1) in the printf statement requires that the variables are initialized outside the while loop.

Such initialization doesn't have to be on the declaration line, but doing it there is simpler.

In contrast, the other variables (e.g. name1), are not used after the while loop, but only inside the loop, and are definitely assigned right before they are used.

like image 66
Andreas Avatar answered Dec 21 '22 08:12

Andreas