Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Confusion in print method in Java

Tags:

java

arrays

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]
like image 594
Stardust Avatar asked Dec 07 '22 04:12

Stardust


1 Answers

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).

like image 68
Thomas Lötzer Avatar answered Dec 09 '22 18:12

Thomas Lötzer