Whenever I try to print the char arrays to the console, I'm getting the result in integer format, but whenever I try to print integer arrays to the console, I'm getting the result in hashcode format. Could anyone please tell me why?
char[] arr={'4','5','6'};
System.out.println(arr); //456
int[] arr={4,5,6};
System.out.println(arr) //[I@3e25a5]
java.io.PrintStream
(the class of System.out
) has a special print
-method for char[]
, but not for int[]
. So for the char[]
, this special method is used, while int[]
is printed via the generic version, which prints the hashcode (or, to be more precise, the result of String.valueOf()
called with the object as parameter).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With