Empty arrays automatically initialize contents?

后端 未结 5 1745
滥情空心
滥情空心 2021-01-16 13:35

How come

int alone;
System.out.println(alone);

Gives errors but

 int[] arr = new int[1];
 System.out.println(arr[0]);
         


        
5条回答
  •  不要未来只要你来
    2021-01-16 14:05

    Yes, for primitive types(except boolean and char) it will be default to ZERO. If object type it will be default to null.

    This java tutorial may help you.

    NOTE: as woot4Moo answered, this is for only instance variables. If local variable, then it won't be default to any.

提交回复
热议问题