java eight data types default values

    技术2022-05-12  10

    Default Values

    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. Relying on such default values, however, is generally considered bad programming style.

    The following chart summarizes the default values for the above data types.

    Data TypeDefault Value (for fields)byte0short0int0long0Lfloat0.0fdouble0.0dchar'/u0000'String (or any object)  nullbooleanfalse

    Local variables are slightly different; the compiler never assigns a default value to an uninitialized local variable. If you cannot initialize your local variable where it is declared, make sure to assign it a value before you attempt to use it. Accessing an uninitialized local variable will result in a compile-time error.


    最新回复(0)