File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

differences between character array and string

 
Soumya Padhiary
Greenhorn
Posts: 20
Eclipse IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4660
5
Linux Mac OS X VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
Eclipse IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4660
5
Linux Mac OS X VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20177
25
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47242
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic