r/javahelp • • Sep 17 '26

Uninitialized variables are null or zero?

Inside public static void main(String[] args) and static void namehere() {}.

Int X;

would be "Uninitialized" and give errors when compiling.

Outside main and any other method, right after public class xxx.

public static int x;

holds no value whatsoever, yet the program doesn't complains about it when compiling, when printing it gives 0 as an int rather than null or NaN.


I am expecting:

Strings: "Null", Null or "".

Int: Null, None, NaN.

booleans give false by default.


What's different in this language?


Edit:

public static int X;

public static boolean Check;



static void Method A() {

X = 1

}



static void Method B() {
    
    boolean ThisLoop = true;
    
    String AverageText = "Average";
    
    int Number;
    
    if (Check == false) {
    
       Number = X;
      
    }

    int Average;
    
    while (ThisLoop == true) {

             try {
                
             Average = Integer.parseInt(JOptionPane.showInputDialog("Input " + AverageText + 

             Integer.toString(Number)));
             
             if (Average >= 0 || Average <= 0) { 
                 
             Number = Number++;
             
             InputNumber = Average;
                 
             ThisLoop = false;
             
             }
                
             }
             
            catch (NumberFormatException e) {
        
            JOptionPane.showMessageDialog(null, "Not a number", "Alert: Stupidity detected", 

            JOptionPane.ERROR_MESSAGE);
        
            ThisLoop = true;
            
           
            }
             
}                            

}

According to Java, we already know booleans with no value are by default false.

I am initializing Number before the If.

Check is public and also is false.

It goes into the If and sets Number to X (Which is public)

Initializes Average.

Sets Average to [Option dialog] "Input" + AverageText + Converts Number into a String.

Java is telling me that Number is not initialize inside the Integer.toString()

If Average is 1000 to 0 or to -1000 then it sums Number + 1.

Java isn't telling me Number = Number++; are not initialized.

7 Upvotes

16 comments sorted by

View all comments

1

u/Edith_0901 Sep 18 '26

Has how everyone talked about the what primitives hold vs what does objects (Integer, Double) holds. I won't talk on this, but just remember this:
- Primitives -> 0
- Wrapper Classes -> null
- float/double can have NaN as a value, but their default is still 0.0.

Now coming to the actual question, Why do some variables get default values and local variables don't?
I think the easiest way to understand this is with an example

class Score{
int matchPoints;
}

Whenever you create a "Score" object, Java has to create that object in a usable state. You don't have to do:

Score score = new Score();
score.matchPoints = 0;

Thus, Java assigns default value for you.
Same thing with static fields, they also get their default values.

But local variables are little different:

void printSomething() {
int number;
int result = 10 / number; //compilation error
}

Java doesn't assume that number is 0. The compiler basically says, "You declared it, but you haven't actually given it a value yet".

And this becomes even more important in your case, where you declared

if (check == false) {
Number = X;
}

Since condition failed, the Number is not assigned. Instead of silently giving it some default and potentially hiding a bug, Java makes you initialize it yourself.

That's basically definite assignment, before a local variable is read, the compiler wants to be sure that it has definitely been assigned.