Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>At a practical level, the main difference between <code>String</code> and <code>char[]</code> is that the instances of the former are immutable, and instances of the latter are mutable. And of course, the String API offers a wide range of useful methods string manipulation methods. </p> <p>So lets talk about the linguistic similarities and differences. </p> <p>First and foremost, (notwithstanding what you may have heard) strings and array instances in Java <em>are both objects</em>. According to the Java Language Specification:</p> <blockquote> <p>4.3.1 Objects An object is a class instance or an array.</p> <p>The reference values (often just references) are pointers to these objects, and a special null reference, which refers to no object.</p> </blockquote> <p>... where the class of a string is <code>java.lang.String</code>.</p> <p>The linguistic distinction between arrays and other object types is that the type of an array is not a normal Java class. For example:</p> <ul> <li>Array types are declared with a different syntax to normal classes.</li> <li>Array instances are created with a different syntax to normal class instances. </li> <li>Array types cannot be named at the Java source code level.</li> <li>You cannot declare a subtype / subclass of an array type.</li> </ul> <p>But all array types are (direct) subtypes of <code>java.lang.Object</code>, which means that you can (for example) assign an array to a variable of type <code>Object</code>, and invoke methods in the Object API. (And there are some interesting things that you can do with these methods to demonstrate the "object-ness" of an array ... but I digress)</p> <p>So what about strings? As mentioned above, a "string" is a normal object which is an instance of the class <code>java.lang.String</code>. There is nothing unusual about this class from a linguistic perspective. It is declared as "final" so that you cannot declare subtypes, but that is not unusual.</p> <p>The thing that makes <code>String</code> a bit special compared with other classes is that the Java language provides some linguistic constructs to support strings:</p> <ul> <li>There is a special <code>String</code> literal syntax for obtaining strings whose content can be determined at compile time.</li> <li>The '+' operator is overloaded to support <code>String</code> concatenation.</li> <li>From Java 7 onwards, the <code>switch</code> statement supports switching on <code>String</code> values.</li> <li>The Java Language Specification defines/assumes that the java.lang.String class has certain properties and methods; e.g. that strings are mutable, that there is a <code>concat</code> method, that string literals are "interned".</li> </ul> <p>By the way, the answer that said that all string instances are held in a string pool is incorrect. Strings are only put in the pool when they interned, and this only happens automatically for string literals and for strings whose values can be calculated at compile-time. (You can force a string instance to be interned by calling the <code>String.intern()</code> method, but this is bit expensive, and not generally a good idea.)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload