Strange toCharArray() behavior

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?

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.


The first line calls the method

print(char[] s) 

on the PrintStream which prints what you expect. The second one calls the method

print(String s)

Where is concatenating the string with the toString implementation of the array which is that ugly thing you get ([C@19821f).


Arrays are objects, and its toString methods returns

getClass().getName() + "@" + Integer.toHexString(hashCode())

In your case [C@19821f means char[] and @19821f is its hashcode in hex notation.

If you want to print values from that array use iteration or Arrays.toString method.

`System.out.println(Arrays.toString(HEX_CHARS));`

The strange output is the toString() of the char[] type. for some odd reason, java decided to have a useless default implementation of toString() on array types. try Arrays.toString(HEX_STRING) instead.


Need Your Help

Custom UITableView Section not increasing height

ios iphone objective-c uitableview

I'm trying to make a custom TableView Section. That is higher than the normal section header.

Debugging does not show current stored procedure version

sql-server debugging sql-server-2008-r2 sql-server-2008

I'm working on a SQL Server 2008 R2 instance. I've just changed the code for a stored procedure. When I try to debug that proc, the SSMS debug window shows the older version of the code.