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?

Était-ce utile?

La solution

It knows that because that's how it's specified. Default value for int is 0, and default value for String is null.

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top