default values for primitives

前端 未结 3 574
野性不改
野性不改 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:17

    for int x the default value would be 0

    For primitive types please refer to this link

    and for int[] x would be null

    0 讨论(0)
  • 2020-12-11 18:31

    Instance variables will be defaulted to a 'reasonable' value. Local variables will contain garbage.

    Read up on the topic here.

    0 讨论(0)
  • 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)
    
    0 讨论(0)
提交回复
热议问题