File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes differences between character array and string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "differences between character array and string" Watch "differences between character array and string" New topic
Author

differences between character array and string

Soumya Padhiary
Greenhorn

Joined: Jan 10, 2013
Posts: 20

What are the main differences between character array and string, as both stores character sequence ?
And which is more efficient in manipulation and why ?

For any reply thanks in advance.
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4659
    
    5

A String has lots of built in methods that are used all the time. A Character array, i.e. Character[] has none of these. Plus, you can concatenate two Strings with a natural syntax.

Inside the String class, there is storage for the Characters, so there are unlikely to be any "performance" differences of any consequence. Its best to focus on writing code that is clear and understandable to humans, and let the optimizer worry about efficiency.
Soumya Padhiary
Greenhorn

Joined: Jan 10, 2013
Posts: 20

Pat Farrell wrote:A String has lots of built in methods that are used all the time. A Character array, i.e. Character[] has none of these. Plus, you can concatenate two Strings with a natural syntax.

Inside the String class, there is storage for the Characters, so there are unlikely to be any "performance" differences of any consequence. Its best to focus on writing code that is clear and understandable to humans, and let the optimizer worry about efficiency.

There is no answer about the differences between the String and Character[] .
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4659
    
    5

Soumya Padhiary wrote:There is no answer about the differences between the String and Character[] .


I have no idea what you are trying to say here.

Are you saying you don't like my answer?
or that you think there is no difference?
or ???
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18987
    
    8

Pat Farrell wrote:A String has lots of built in methods that are used all the time. A Character array, i.e. Character[] has none of these. Plus, you can concatenate two Strings with a natural syntax.


Soumya, this is the answer which apparently you missed.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40034
    
  28
You will find a bit about this question in the Java Language specification. In the C language, Strings are all implemented as arrays of chars (often written as *char). In C you would implement it like thisNote it contains 9 memory locations, 123456-123464. You can change the last letter like this:- *(myString + 7) = 't'; and you can change the memory like this *(myString + 9) = 'x'; which changes the memory location one beyond the \0 That is a fruitful source of serious errors.
Fortunately none of that code will even compile in Java.
In Java a String is a full‑blown object of a full‑blown class. It has a char[], but it is hidden where you cannot change it. The String class has lots of methods (as you have already been told). You can get a copy of the array with the toCharArray() method.
A char in C occupies 8 bits and a Java char 16 bits.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: differences between character array and string