At the practical level, the main difference between String and char[] is that the instances of the former are immutable, and the instances of the latter are mutable. And of course, the String API offers a wide range of useful string manipulation methods.
So let's talk about linguistic similarities and differences.
First of all, (despite what you heard), string and array instances in Java are both objects. According to the Java language specification:
4.3.1 Objects An object is an instance of a class or an array.
Indicative values ββ(often just links) are pointers to these objects and a special null reference that does not apply to the object.
... where is the java.lang.String string class.
The linguistic difference between arrays and other types of objects is that the type of the array is not a normal Java class. For instance:
- Array types are declared with a different syntax for regular classes.
- array instances are created with a different syntax for regular class instances.
- Array types cannot be named at the Java source code level.
- You cannot declare a subtype / subclass of an array type.
But all types of arrays are (direct) subtypes of java.lang.Object , which means that you can (for example) assign an array to a variable of type Object and call methods in the object's API. (And there are some interesting things you can do with these methods to demonstrate the "objectivity" of the array ... but I'm distracted)
What about strings? As mentioned above, a string is an ordinary object that is an instance of the java.lang.String class. There is nothing unusual in this class from a linguistic point of view. It is declared as "final", so you cannot declare subtypes, but this is not unusual.
What makes String little special compared to other classes is that the Java language provides some linguistic constructs to support strings:
- There is a special
String syntax for getting strings whose contents can be determined at compile time. - The '+' operator is overloaded to support
String concatenation. - Starting in Java 7, the
switch supports the inclusion of String values. - The Java language specification defines / assumes that the java.lang.String class has certain properties and methods; for example, that strings are mutable, that there is a
concat method, that string literals are interned.
By the way, the answer, which says that all line instances are stored in the row pool, is incorrect. Strings are only placed in the pool when they are interned, and this happens automatically only for string literals and for strings whose values ββcan be computed at compile time. (You can force an instance of a string to be interned by calling the String.intern() method, but this is a bit expensive and not a good idea at all.)