default values for primitives

前端 未结 3 576
野性不改
野性不改 2020-12-11 17:52

In Java, what would a variable of type int hold if it was not initialized (I know that it will not let me compile if I used x directly before initi

3条回答
  •  抹茶落季
    2020-12-11 18:32

    x in "int x" is automatically initialized as 0; x in "int [] x" is automatically initialized as null, since x is actually a reference

    but compiler will prompt to initialize the variable:

    Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
        The local variable a may not have been initialized
        The local variable a may not have been initialized
    
        at initialization.main(initialization.java:6)
    

提交回复
热议问题