Search code examples
javaarraysstringintegerchar

Difference of outputs of print of int[] ,String[] and char[] in Java


My question is simple .I know output but i can't know why is different outputs of all of them.

Firstly, i wanted to get output of array what is created newly and is empty .i coded like that:

(https://i.sstatic.net/V40Jw.png)

and i got output like that:

(https://i.sstatic.net/6cJdU.png)

I hope you will take into account that I am just learning Java 1 month


Solution

  • Here you have created an array of integers with a length of 2 using new int[2]. In Java, when you initialize an array of integers using new int[2], it initializes each element to its default value, which is 0 for int type. Same goes for arrays of char and string and as well you have not intitialized any of the arrays that is why you are getting their respective default values as output .