Java CharSequence[] 和 String[] 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3600635/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 02:29:45  来源:igfitidea点击:

What is the difference between CharSequence[] and a String[]?

java

提问by Brigadier

What is the difference between CharSequence[]and String[]?

CharSequence[]和 和有String[]什么区别?

回答by codinguser

A CharSequence is an Interface. String is an immutable sequence of characters and implements the CharSequence interface. CharSequence[] and String[] are just arrays of CharSequence and String respectively.

CharSequence 是一个接口。String 是一个不可变的字符序列并实现 CharSequence 接口。CharSequence[] 和 String[] 分别只是 CharSequence 和 String 的数组。

This means wherever you see CharSequence, you can pass a String object.

这意味着无论您在何处看到 CharSequence,都可以传递一个 String 对象。

回答by Thierry-Dimitri Roy

Stringimplements the CharSequenceinterface. CharSequence is implemented by String, but also CharBuffer, Segment, StringBuffer, StringBuilder.

String实现CharSequence接口。CharSequence由String实现,还有CharBuffer、Segment、StringBuffer、StringBuilder。

So a String[] and a CharSequence[] is essentially the same. But CharSequence is the abstraction, and String is the implementation.

所以 String[] 和 CharSequence[] 本质上是一样的。但 CharSequence 是抽象,而 String 是实现。

By the way, '[]' denotes an arrayof objects. So String[]is an array of strings. And String itself is an array of characters.

顺便说一下,'[]' 表示一个对象数组String[]字符串数组也是如此。而 String 本身就是一个字符数组。

回答by Igor

CharSequencerepresents an ordered set of characters, and defines methods for examining this character set. It is ineterface, and one implementation of this interface is Stringclass.

CharSequence表示一组有序的字符,并定义检查此字符集的方法。它是一个接口,这个接口的一个实现是String类。

Please refer to Java API documentation for further info. Also, this tutorial might help you: http://download.oracle.com/javase/tutorial/

有关更多信息,请参阅 Java API 文档。此外,本教程可能对您有所帮助:http: //download.oracle.com/javase/tutorial/

回答by Chinmay Kanchi

CharSequencerepresents an interface to a sequence of characters, with operations common to all classes implementing it. However, in particular, CharSequencedoes not make any guarantees about whether the sequence is mutable or not. So, you can have an immutable implementing class, like Stringor mutable ones, like StringBuilderand StringBuffer.

CharSequence表示一个字符序列的接口,具有实现它的所有类的通用操作。但是,特别CharSequence是对序列是否可变不做任何保证。所以,你可以有一个不可变的实现类,比如String或可变的,比如StringBuilderStringBuffer

In addition, CharSequencedoes not refine the general purpose implementations of the equals()or hashCode()methods, so there is no guarantee that objects of different classes implementing CharSequencewill compare to be equal even if the underlying sequence that they hold is the same. So, given:

此外,CharSequence没有细化equals()hashCode()方法的通用实现,因此CharSequence即使它们持有的底层序列相同,也不能保证实现的不同类的对象比较相等。所以,给定:

String seq1 = "hello";
StringBuilder seq2 = new StringBuilder("hello");
StringBuffer seq3 = new StringBuffer("hello");

comparisons between these three using .equals()return falseon Java 1.6, but I can't find any guarantees that this will not change in the future (though it's probably fairly unlikely).

在 Java 1.6 上使用.equals()return对这三个进行比较false,但我找不到任何保证将来不会改变的保证(尽管这可能不太可能)。

And CharSequence[]and String[]are just arrays of their respective types.

并且CharSequence[]String[]只是它们各自类型的数组。

EDIT: The practical upshot of this is to compare CharSequences for equality, you have to use their toString()method and compare the resultant Strings, since this is guaranteed to return trueif the underlying sequences are the same.

编辑:这样做的实际结果是比较CharSequences 是否相等,您必须使用他们的toString()方法并比较结果Strings,因为true如果底层序列相同,则保证返回。

回答by user28775

CharSequence is an interface. String is a class that implements the CharSequence interface. That means a String object passes as a CharSequence object. There are different classes that implement the CharSequence interface. They all define the methods that correlate with the method signatures (or abstract methods?) that the interface CharSequence provides.

CharSequence 是一个接口。String 是一个实现 CharSequence 接口的类。这意味着 String 对象作为 CharSequence 对象传递。有不同的类可以实现 CharSequence 接口。它们都定义了与 CharSequence 接口提供的方法签名(或抽象方法?)相关的方法。

回答by Basil Bourque

Here is my diagram showing how Stringis a one of several concrete classesimplementing the CharSequenceinterface.

这是我的图表,显示了如何实现接口String的几个具体类之一。CharSequence

In particular, StringBuilderis commonly used as a faster alternative to concatenation Stringobjects with the +operator, where thread-safety is not needed.

特别是,StringBuilder通常用作使用运算符连接String对象的更快替代方法+,其中不需要线程安全。

The square brackets means an array. So, String[]is an array of Stringobjects, and CharSequence[]is an array of objects that may or may not be Stringobjects but definitely are objects implementing the CharSequenceinterface.

方括号表示数组。所以,String[]是一个String对象数组,是一个对象CharSequence[]数组,可能是也可能不是String对象,但肯定是实现CharSequence接口的对象。

See my Answer hereand my Answer herefor much more discussion.

有关更多讨论,请参阅此处的我的答案此处的我的答案

回答by PRao

String is the parent class that implements the interface CharSequence.

String 是实现接口的父类CharSequence

There are other classes which also implement the CharSequencelike StringBuffer, StringBuilder, etc.

有也实现其他类CharSequence一样StringBufferStringBuilder等等。