It knows that because that's how it's specified. Default value for int
is 0, and default value for String
is null.
Dummy values of serialized variable
-
11-04-2022 - |
Question
Why default values as Zero are displayed for transient variables in Java?
How does it know that the value should be 0
for integer
and null
for String
?
La solution
Autres conseils
From DOCS
It's not always 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. Generally speaking, this default will be zero or null, depending on the data type.
Default value of an int is 0 and a String is null
The default values for fields is defined by the JLS. Making it transient gives a hint that it won't be set and thus have the default value.
How does it know that the value should be 0 for integer and null for String?
Because that's the default, defined in the Java Language Specification.