Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strange toCharArray() behavior

Tags:

java

arrays

char

I was experimenting with toCharArray() and found some strange behavior.

Suppose private static final char[] HEX_CHARS = "0123456789abcdef".toCharArray();

 System.out.println(HEX_CHARS);

  /* prints 0123456789abcdef */

 System.out.println("this is HEX_CHARS "+HEX_CHARS); 
  /* prints [C@19821f */

Any theoretical reason behind this?

like image 620
jmishra Avatar asked Aug 05 '26 21:08

jmishra


1 Answers

It is because the parameter to println is different in the two calls.

The first parameter is called with char[] and the second is called with a string, where HEX_CHARS is converted with a call to .toString().

The println() have an overriden method that accepts a charArray.

like image 133
MAV Avatar answered Aug 07 '26 12:08

MAV



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!