Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Java, is a String an array of chars?

Tags:

java

string

I want to know, if a String is a collection. I have read around but am still quite confused.

like image 220
juju Avatar asked Nov 27 '13 16:11

juju


1 Answers

Strings are immutable objects representing a character sequence (CharSequence is one of the interfaces implemented by String). Main difference to char arrays and collections of chars: String cannot be modified, it's not possible (ignoring reflection) to add/remove/replace characters.

Internally they are represented by a char array with an offset and length (this allows to create lightweight substrings, using the same char arrays).

Example: ['F','o','o','H','e','l','l','o',' ','W','o','r','l','d'], offset=3, count=5 = "Hello".

like image 71
Peter Walser Avatar answered Sep 20 '22 18:09

Peter Walser