The difference between a string and an array of characters

How do these declarations differ from each other?

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

How about memory allocation in each case?

+4
source share
7 answers

To fix a compilation error, replace it with one of the following char[] statements

 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"); 

The next section compares the above statement with each other.

String constant

 String s="MY PROFESSION"; 

Character array

  char c1[]="MY PROFESSION".toCharArray(); char c2[]={'M', 'Y', ' ', 'P', 'R', 'O', 'F', 'E', 'S', 'S', 'I', 'O', 'N'}; 
  • c1 contains a copy of the underlying String array (via System.arraycopy ) and is stored in heap space
  • c2 is created on the fly in the stack frame, loading individual character constants.
  • c1 and c2 are mutable, i.e. the contents of Array can be changed. c2[0]='B'
  • Array size / length fixed (cannot be added)

StringBuilder / StringBuffer

 StringBuilder sb = new StringBuilder("MY PROFESSION"); StringBuffer sbu = new StringBuffer("MY PROFESSION"); 
  • Both sb and sbu mutable. sb.replace(0, 1, 'B');
  • Both sb and sbu are stored on the heap
  • Size / length can be changed. sb.append( '!');
  • StringBuffer methods are synchronized, and StringBuilder methods StringBuilder not
+7
source

The first is compiled. The second is not.

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

A String is an object of type java.lang.String that contains many useful methods for managing strings. Inside, it uses a char array.

Another important feature of String is that it is immutable. You can pass String to any method and make sure that this method does not change the contents of String. This does not apply to the char array.

As far as memory is concerned, String consumes a few more bytes, but usually this is not something that should be guided by your design decisions: usually using the char array is not what you should do.

+3
source

The character array is changed, in other words, you can replace the character in the character array by rewriting the memory location for that character.

A string is not mutable, in other words, to "replace" a character with a String, you must build a new String with the desired character in place (copying non-changing parts from the old string).

Although this seems simple, it has a profound effect on the ability to share between Thread (and objects). One string can be safely exchanged between threads without additional checks to see if the string is being modified (which could lead to Thread inconsistencies for String).

Other optimizations are possible, since Strings cannot mutate, you can flip equality operations equal to "by value". This means that a "String factory" can return cached copies of the same string, even if two different String objects are requested, since it will not be possible to behave differently for two objects.

+1
source

char is a primitive type. String is a class in which the actual data is stored inside an array of characters.

 char c[]="MY PROFESSION"; 

will give a compilation error.

An array of characters is a continuous storage in memory where characters are stored sequentially.

More on this Thread .

0
source

If you see docs ,

  String str = "abc"; is equivalent to: char data[] = {'a', 'b', 'c'}; // not 'abc' String str = new String(data); 

More String literals are very specific in java

String supported by the character internal array.

0
source

How is this difference different from each other?

Your first line creates an instance of String . Your second line is simply invalid; perhaps you meant:

 char[] c = {'M', 'Y', ' ', 'P', 'R', 'O', 'F', 'E', 'S', 'S', 'I', 'O', 'N'}; 

which creates a char[] filled with these characters.

how to allocate memory?

Saving a string as a String slightly different from memory than saving it as a char[] . However, there are similarities: both are objects and have ordinary office expenses.

However, a String contains char[] inside, so naturally, String consumes more memory. In addition, String has 3 int fields ( offset , count and hash ), while char[] has one int length field.

For example, saving "MY PROFESSION" as a String :

  • 3 int fields: 12 bytes
  • char[] field: 8 bytes
    • int field: 4 bytes
    • service data of the object: 8 bytes
    • 13 characters: 26 bytes
  • service data of the object: 8 bytes

This is up to about 66 bytes. I say "o" because part of this depends on the virtual machine. The corresponding char[] length 10 consumes only 38 bytes, as you can see. The memory difference here is very slight, so you should not worry about that (and just use String !). This becomes even more insignificant the longer the row you are trying to save is stored.

0
source

char is a primitive type and String is a class. An array of a given number of lines and strings is a set of characters.

0
source

Source: https://habr.com/ru/post/1497574/


All Articles