Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing char arrays in Java

Tags:

java

arrays

If I print out a char array normally

char[] c = new char[]{'3','b','o'};
System.out.println(c); //prints 3bo

the output is a string. However, if I concatenate a String it prints the array memory location.

System.out.println("" + c); //prints [C@659e0bfd

I would have expected it to print as a String and I'm sure why this behavior happens. Any explanations?

like image 382
Toby L. Avatar asked Apr 01 '16 22:04

Toby L.


1 Answers

Simplest form is:

Arrays.toString(c);

and if it is a 2D array, then use Arrays.deepToString as follows:

Arrays.deepToString(twoDArray);
like image 193
KayV Avatar answered Sep 21 '22 15:09

KayV