Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are Java String Objects an Array of Chars?

I am new to java and trying to understand the essentials and fundamentals of the language.

Is it accurate to state that Java string objects are intrinsically a class defined as an immutable array of chars?

I ask this as I'm a bit confused by the spec in comparison to char arrays and the string class...

JLS 10.9

10.9 An Array of Characters is Not a String In the Java programming language, unlike C, an array of char is not a String, and neither a String nor an array of char is terminated by '\u0000' (the NUL character). A String object is immutable, that is, its contents never change, while an array of char has mutable elements. The method toCharArray in class String returns an array of characters containing the same character sequence as a String. The class StringBuffer implements useful methods on mutable arrays of characters.

JLS 4.3.3

4.3.3 The Class String Instances of class String represent sequences of Unicode code points.

like image 593
Edward J Beckett Avatar asked Nov 02 '12 19:11

Edward J Beckett


People also ask

Is a Java string an array of chars?

char is a primitive data type whereas String is a class in java. char represents a single character whereas String can have zero or more characters. So String is an array of chars.

Is a string just an array of chars?

String refers to a sequence of characters represented as a single data type. Character Array is a sequential collection of data type char. Strings are immutable. Character Arrays are mutable.

Are strings just arrays Java?

No, a String is an immutable string of characters, and the class extends Object directly and does not implement any of the Collection interfaces.

What is array of string objects in Java?

A String Array is an Array of a fixed number of String values. A String is a sequence of characters. Generally, a string is an immutable object, which means the value of the string can not be changed. The String Array works similarly to other data types of Array.


1 Answers

Is it accurate to state that Java string objects are intrinsically a class defined as an immutable array of chars?

No. A Java String object is (currently - it's an implementation detail which I gather may be changing) a class containing a few fields:

  • A char[] containing the actual characters
  • A starting index into the array
  • A length
  • A cached hash code, lazily computed

The reason for the index and length is that several strings can contain references to the same char[]. This is used by some operations such as substring (in many implementations, anyway).

The important thing is the API for String though - which is very different to the API for an array. It's the API you would think of when you take the JLS definition into account: a String represents a sequence of Unicode code points. So you can take a subsequence (Substring), find a given subsequence (indexOf), convert it to an upper case sequence etc.

In fact the JLS would be slightly more accurate to call it a sequence of UTF-16 code units; it's entirely possible to construct a string which isn't a valid sequence of Unicode code points, e.g. by including one half of a "surrogate pair" of UTF-16 code units but not the other. There are parts of the API which do deal with the String in terms of code units, but frankly most developers spend most of the time treating strings as if non-BMP characters didn't exist.

like image 140
Jon Skeet Avatar answered Sep 30 '22 01:09

Jon Skeet