If you want to print String which should be based on array of character then you should wrap this array with new String object. So instead of
String b = ar.toString();
use
String b = new String(ar);
You need to know that arrays inherit toString()
method from Object so its code returns
getClass().getName() + "@" + Integer.toHexString(hashCode());
which means you will see [C
as result of getClass().getName()
which represents one dimensional array of char
acters, @
token and hexadecimal form of arrays hexcode 22a79c31
.
In case you would want to print content of array with different type of data than char
you wouldn't be able to wrap it in String. Instead you will have to iterate over each elements and print them. To avoid writing your own method for this Java gives you java.util.Arrays
class with toString(yourArray)
method which will iterate over each elements of array and generate String in form
[element0, element1, element2, ... , elementN-1]