Ninad Gosavi Ninad Gosavi - 11 months ago 61
Java Question

Why can't we use toString() to convert char [] to String ? And what does toString do with Char [] object?

String str="aabcd";

char [] ca = str.toCharArray();

//Some manipulations on char [] ...

String str2 = ca.toString();

System.out.println("String is " +str2);

This gives object reference as output ([C@1db9742 ) .

Answer Source

Why can't we use toString() to convert char [] to String?

There is no special Class called char[], Array's are native objects and their type really depend on what data type you used while using it.

Hence ovveriding it's toString() method is highly difficult to give you something in string format back.

Consider a case where you using an array like int[], what should toString() give back ?

Hence the authors leave it as it gives you the default behaviour of Object class.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download