How are these declarations different from each other?
String s="MY PROFESSION";
char c[]="MY PROFESSION";
What about the memory allocation in each case?
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.
char represents a single character whereas String can have zero or more characters. So String is an array of chars.
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.
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.
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 s="MY PROFESSION";
s
is immutable i.e content of String
is intact can not be modified. 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 spacec2
is built on the fly in the stack frame by loading individual character constantsc1
& c2
are mutable i.e content of Array
can be modified. c2[0]='B'
StringBuilder sb = new StringBuilder("MY PROFESSION");
StringBuffer sbu = new StringBuffer("MY PROFESSION");
sb
and sbu
are mutable. sb.replace(0, 1, 'B');
sb
and sbu
are stored in heapsb.append( '!');
StringBuffer
's methods are synchronised while StringBuilder
's methods are notThe 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With