sidharth sharma
sidharth sharma

Reputation: 3215

Why does the toString method in java not seem to work for an array

I want to convert a character array to a string object using the toString() method in java. Here is a snippet of the test code I used:

import java.util.Arrays;
class toString{
    public static void main(String[] args){
        char[] Array = {'a', 'b', 'c', 'd', 'e', 'f'};
        System.out.println(Array.toString());
        }
}

In principle, it should print abcdef, but it is printing random gibberish of the likes of [C@6e1408 or [C@e53108 each time the program executes. I don't need an alternative out of this but want to know why this is happening.

Upvotes: 38

Views: 49214

Answers (9)

Harshul
Harshul

Reputation: 1

There is a spelling mistake of "Array.toString()" to "Arrays.toString(Array)" I guess so, and instead of writing name.toString(), pass the name as an argument and Write as above.

Upvotes: 0

Khanh Tran
Khanh Tran

Reputation: 35

char[] Array = { 'a', 'b', 'c', 'd', 'e', 'f' };
System.out.println(Array);

It should print abcdef.

Upvotes: -1

Bohemian
Bohemian

Reputation: 424983

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".

Upvotes: 46

jhkj
jhkj

Reputation: 21

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 !!

Upvotes: 2

Brigham
Brigham

Reputation: 14524

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));
    }
}

Upvotes: 2

Swaranga Sarma
Swaranga Sarma

Reputation: 13403

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.

Upvotes: 0

Redsmurf
Redsmurf

Reputation: 1

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;
}

Upvotes: -3

Jon7
Jon7

Reputation: 7215

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.

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1499860

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[]).

Upvotes: 38

Related Questions