Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between a string and an array of characters

Tags:

java

How are these declarations different from each other?

String s="MY PROFESSION";
char c[]="MY PROFESSION";

What about the memory allocation in each case?

like image 429
Shivaji More Avatar asked Aug 17 '13 12:08

Shivaji More


People also ask

What is the difference between an array of characters and a string in C?

Key Differences Between Character Array and StringA character array is a collection of variables which are of character datatype. String is a class that is instantiated to declare strings. Using index value you can access a character from a character array.

Is a string an array of characters?

char represents a single character whereas String can have zero or more characters. So String is an array of chars.

Why is character array better than string?

Since String is immutable, there is no method defined that allow us to change or overwrite the content of the string. This feature makes string objects unstable for storing secure information such as passwords, SSN, etc. We should always store the secure information in char[] array rather than String.

What is the difference between a string and a character?

The main difference between Character and String is that Character refers to a single letter, number, space, punctuation mark or a symbol that can be represented using a computer while String refers to a set of characters. In brief, String is a collection of characters.


2 Answers

To correct compilation error replace with one of the below char[] statement

String s = "MY PROFESSION";
char c1[] = "MY PROFESSION".toCharArray();
char c2[] = { 'M', 'Y', ' ', 'P', 'R', 'O', 'F', 'E', 'S', 'S', 'I', 'O', 'N' };
StringBuilder sb = new StringBuilder("MY PROFESSION");
StringBuffer sbu = new StringBuffer("MY PROFESSION");

Following section compares above statement with each other

String Constant

String s="MY PROFESSION";
  • "MY PROFESSION" is a constant and stored in String pool see Where does Java's String constant pool live, the heap or the stack?
  • s is immutable i.e content of String is intact can not be modified.
  • Size/Length of string is fixed (not possible to append)

Character Array

 char c1[]="MY PROFESSION".toCharArray();
 char c2[]={'M', 'Y', ' ', 'P', 'R', 'O', 'F', 'E', 'S', 'S', 'I', 'O', 'N'};
  • c1 holds copy of String's underlying array (via System.arraycopy) and stored in heap space
  • c2 is built on the fly in the stack frame by loading individual character constants
  • c1 & c2 are mutable i.e content of Array can be modified. c2[0]='B'
  • Size/Length of Array is fixed (not possible to append)

StringBuilder / StringBuffer

StringBuilder sb = new StringBuilder("MY PROFESSION");
StringBuffer sbu = new StringBuffer("MY PROFESSION");
  • Both sb and sbu are mutable. sb.replace(0, 1, 'B');
  • Both sb and sbu are stored in heap
  • Size/Length can be modified. sb.append( '!');
  • StringBuffer's methods are synchronised while StringBuilder's methods are not
like image 118
Prashant Bhate Avatar answered Sep 18 '22 11:09

Prashant Bhate


The first one compiles. The second one doesn't.

A char[] is just that: an array of primitive numbers of type char. All it provides is a length attribute, and a way to get and set a char at a given index.

A String is an object, of type java.lang.String, which has a whole lot of useful methods for manipulating Strings. Internally, it uses a char array.

Another important feature of String is that it's immutable. You can pass a String to any method and be sure that this method won't change the contents of the String. That is not the case with a char array.

Regarding memory, a String consumes some more bytes, but that's generally not what should guide your design decisions: generally, using a char array is not what you should do.

like image 32
JB Nizet Avatar answered Sep 22 '22 11:09

JB Nizet