int array initialization

前端 未结 7 2131
暗喜
暗喜 2020-12-04 20:15

I have here a simple question related to Java. Let\'s say you have an int array as instance variable:

int[] in = new int[5];

So, now by def

7条回答
  •  旧时难觅i
    2020-12-04 20:33

    Yes, when you initialise an array the contents will be set to the default value for that type, for int it would be 0 and for a reference type it would be null.

    If you initialise an array and inspect the contents you can see this for yourself:

    ...
    final int[] in = new int[5];
    
    for (int i = 0; i < in.length; i++) {
        System.out.println(in[i]);
    }
    ...
    

    This will print:

    0
    0
    0
    0
    0
    

提交回复
热议问题