aspose file tools*
The moose likes Java in General and the fly likes Creating String from character array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Creating String from character array" Watch "Creating String from character array" New topic
Author

Creating String from character array

chander shivdasani
Ranch Hand

Joined: Oct 09, 2007
Posts: 206

Hi,

I have a small question. I was writing a program in which i had to convert a character array to string. I took the following approach:

chars.toArray();

where, chars is a character array. I got some garbage value in return. Then i did:

String str = new String(chars);

And got the right string back. However, i didnt understand the difference between both. In my opinion, they should return the same value, shouldnt they?



Enjoy, Chander
SCJP 5, Oracle Certified PL/SQL Developer
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3018
    
  10
chander shivdasani wrote:I took the following approach:

chars.toArray();

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?
chander shivdasani
Ranch Hand

Joined: Oct 09, 2007
Posts: 206

I am really sorry.
I actually meant chars.toString()
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3018
    
  10
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"?
chander shivdasani
Ranch Hand

Joined: Oct 09, 2007
Posts: 206

Thats what even i said that chars.toString() is returning some garbage value. I wanted to know why?

The following is the code that i wrote to test the difference between them:

Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3018
    
  10
Ah, now the question starts to make sense.

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?
chander shivdasani
Ranch Hand

Joined: Oct 09, 2007
Posts: 206

Thanks mike, Now it all makes sense to me
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Creating String from character array