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 bytesint 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.
source share