Issue
I was wondering if it was possible, in Java, to let a field expected to be filled with an integer (such as in a JTable), empty. I mean something like " "
for the Strings. I tried with null but it's not allowed for integers. Of course, I could put a default value s.a. 0
but I don't want. Could you please help me?
Solution
It is not necessary to assign a value when a field is declared. Fields that are declared but not initialized will be set to a reasonable default by the compiler (i.e. 0 for numeric types and null for objects).
For local variables the compiler never assigns a default value to an uninitialized local variable. So you must do this manually because in Java you cannot use a local variable that might be uninitialized, where "Uninitialized" means the variable might not have been set to anything.
You can set your variable as needed:
-Object can be set to null;
-Integer can be set to 0, Integer.MAX_VALUE
or Integer.MIN_VALUE
.
Answered By - darioSka
Answer Checked By - Senaida (JavaFixing Volunteer)