chander shivdasani wrote:I took the following approach:
where, chars is a character array.
No, that's not possible. If chars were really a character array, this would not compile, since no array has a toArray() method. I think you need to look again at how chars is declared. Is it possible you have more than one variable named chars?
This still isn't making sense. If you use toString() on an array of any type, you won't get a useful result, I guarantee. I think you need to look again at your actual code, and show us what you're really doing. How is chars declared? How is it assigned a value? How do you know that what you end up with is the "right string"?
Well, the basic problem is that when Java was created, some parts were created quickly, and sometimes poorly. It would have made sense for someone to give arrays a useful toString() method. Unfortunately, they didn't, and we're stuck with the standard implementation inherited from Object. Look at what you see if you do new Object().toString(). Look at the API for toString() given in the Object class. Look familiar? That's what you're dealing with.
On the other hand, does this matter? So some classes and methods don't do what we might expect. Other methods do work. One can spend time wondering about the former, to no avail. Or one can spend time learning to use the latter. Which seems more productive?
Calling toString() on any array gives you no useful information - it returns the class name (which starts with [ for arrays!), followed by @ and the hexadecimal hash code. Use one of the toString methods of java.util.Arrays to convert any array to a String.