Java 字符串对象是字符数组吗? [英] Are Java String Objects an Array of Chars?

查看:104
本文介绍了Java 字符串对象是字符数组吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Java 的新手,并试图了解该语言的本质和基础知识.

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

声明 Java 字符串对象本质上是一个定义为不可变字符数组的类是否准确?

我问这个是因为我对与字符数组和字符串类相比的规范有点困惑......

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 字符数组不是字符串在 Java 编程语言中,与 C 不同,char 数组不是 String,并且字符串和字符数组都不会以 '\u0000'(NUL特点).String 对象是不可变的,也就是说,它的内容永远不会改变,而一个数组char 具有可变元素.类 String 中的 toCharArray 方法返回一个包含字符的数组与字符串相同的字符序列.类 StringBuffer 实现了有用的可变字符数组的方法.

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 类 String 的类 String 实例表示 Unicode 码位序列.

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

推荐答案

声明 Java 字符串对象本质上是一个定义为不可变字符数组的类是否准确?

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

没有.Java String 对象是(目前 - 我收集的一个实现细节可能会改变)一个类包含几个字段:

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

  • 包含实际字符的char[]
  • 数组的起始索引
  • 长度
  • 延迟计算的缓存哈希码

索引和长度的原因是多个字符串可以包含对同一个char[]的引用.这被一些操作使用,例如 substring(无论如何,在许多实现中).

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).

重要的是 String 的 API - 这与数组的 API 非常不同.这是您在考虑 JLS 定义时会想到的 API:String 表示 Unicode 代码点序列.所以你可以取一个子序列(Substring),找到一个给定的子序列(indexOf),把它转换成大写序列等等.

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.

事实上,JLS 将其称为 UTF-16 代码单元序列会稍微准确一些;完全有可能构造一个不是 valid Unicode 代码点序列的字符串,例如通过包含 UTF-16 代码单元的代理对"的一半而不是另一个.API 的某些部分确实在代码单元方面处理String,但坦率地说,大多数开发人员花费大部分 将字符串视为不存在非 BMP 字符.

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.

这篇关于Java 字符串对象是字符数组吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆