ARTICLE AD BOX
To get a human-readable toString(), you must use Arrays.toString(), like this:
System.out.println(Arrays.toString(Array));Java's toString() for an array is to print [, followed by a character representing the type of the array's elements (in your case C for char), followed by @ then the "identity hash code" of the array (think of it like you would a "memory address").
This sad state of affairs is generally considered as a "mistake" with java.
See this answer for a list of other "mistakes".
9 Comments
I don't know where you get the idea that "in principle" it should print "abcdef". Where is that documented?
Something like [C@6e1408 is certainly not random gibberish - it's the same way of constructing a string from an object as any other type that doesn't override toString() inherits - it's a representation of the type ([ indicating an array; C indicating the char primitive type) followed by the identity hash code in hex. See the documentation for Object.toString() for details. As it happens, arrays don't override toString.
If you want [a, b, c, d, e, f] you can use Arrays.toString(char[]). If you want abcdef you can use new String(char[]).
10 Comments
I think 'in principle' it is expected to overwrite the toString() method as it deals with characters and human-readable content which could be outputted to a valid human-readable string. Or at the very least implement something like the Arrays class, as you point out. From the first sentence of the documentation (as I skimmed it) I only saw Returns a String object representing this Character's value. which I expected to mean it overrides toString() + outputs a String object with a length of one; a human readable representation. Obviously my bad for skimming; but still!
2012-11-04T18:04:17.223Z+00:00
@FergusMorrow: That's the documentation for Character.toString. This isn't a single char, it's an array. Basically, you should only rely on toString doing anything particularly useful for classes where it's been overridden, and it isn't overridden for arrays. That's certainly a shame, but the OP had no good reason to expect anything different "in principle" IMO.
2012-11-04T20:53:24.693Z+00:00
If you think you have to take this literally, the OP said “I want to convert a character array to a string object using the toString() method”, so your variants Arrays.toString(char[]) and new String(char[]) are not matching that sentence either. In fact, the question’s last sentence is “I don't need an alternative out of this but want to know why this is happening.”, so any suggestion how to fix it is outside the scope of the question. Or we agree on addendums beyond the literal question being allowed. For most real life tasks, you don’t need to convert a char[] to a String…
2022-07-04T16:44:53.28Z+00:00
Arrays don't override toString. There's a static method: java.util.Arrays.toString that should solve your problem.
import java.util.Arrays; class toString { public static void main(String[] args){ char[] Array = {'a', 'b', 'c', 'd', 'e', 'f'}; System.out.println(Arrays.toString(Array)); } }Just use the following commands to get your abcdef array printed
String a= new String(Array); System.out.println(a);there you have it problem solved !! now regarding why is printing the other stuff i think those guys above put some useful links for that. Ok gotta go !!
Because a char array is an array of primitives and toString() will give you it's default (which is a hash of the object). Some classes will implement toString() to do cooler things, but primitaves will not.
The default implementation of the toString method of the char [] class returns a String representation of the base address of the array, which is what is being printed here. We cannot change it, since the class of char [] is not extendable.
63k13 gold badges187 silver badges231 bronze badges
It should print abcdef.
2 Comments
@ImpulseTheFox there is no prove to your claim that the “OP wants to convert the char[] to a String, and not just print it”. In fact, the OP said explicitly “I don't need an alternative out of this but want to know why this is happening.” so the OP wanted neither, know how to print it nor know how to create a string.
2022-07-04T16:31:30.36Z+00:00
this way I found worked:
public String convertToString(char[] array, int length) { String char_string; String return_string=""; int i; for(i=0;i<length;i++) { char_string=Character.toString(array[i]); return_string=return_string.concat(char_string); } return return_string; }Explore related questions
See similar questions with these tags.


