programing

CharSequence []와 String []의 차이점은 무엇입니까?

nasanasas 2020. 11. 24. 08:04
반응형

CharSequence []와 String []의 차이점은 무엇입니까?


CharSequence[]의 차이점은 무엇입니까 String[]?


StringCharSequence 인터페이스를 구현합니다 . CharSequence는 String뿐만 아니라 CharBuffer, Segment, StringBuffer, StringBuilder에 의해 구현됩니다.

따라서 String []과 CharSequence []는 본질적으로 동일합니다. 그러나 CharSequence는 추상화이고 String은 구현입니다.

그런데 '[]' 는 객체 배열나타냅니다 . String[]문자열 배열도 마찬가지 입니다. 그리고 String 자체는 문자 배열입니다.


CharSequence문자 시퀀스에 대한 인터페이스를 나타내며이를 구현하는 모든 클래스에 공통적 인 작업이 있습니다. 그러나 특히 CharSequence는 시퀀스가 ​​변경 가능한지 여부를 보장하지 않습니다. 그래서, 당신은 같은 불변의 구현 클래스를 가질 수 String와 같은 또는 변경 가능한 것들, StringBuilder하고 StringBuffer.

또한, CharSequence의 범용 구현 구체화하지 않는 equals()또는 hashCode()방법이므로 다른 클래스의 객체가 구현된다는 보장이 없다 CharSequence가 유지하는 기본 시퀀스가 동일한 동일하더라도 것으로 비교 뜻. 그래서 주어진 :

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

Java 1.6에서 .equals()반환 false사용 하여이 세 가지를 비교 했지만, 이것이 앞으로 변경되지 않을 것이라는 보장을 찾을 수 없습니다 (아마도 거의 불가능할 것 같음).

그리고 CharSequence[]하고 String[]각각의 유형의 단지 배열입니다.

편집 : 이것의 실질적인 결과는 CharSequences가 같음을 toString()비교하는 것 입니다. 기본 시퀀스가 ​​동일하면 String반환 true되는 것이 보장되기 때문에 방법 을 사용하고 결과 s를 비교 해야합니다.


CharSequence는 인터페이스입니다. 문자열은 변경 불가능한 문자 시퀀스이며 CharSequence 인터페이스를 구현합니다. CharSequence []와 String []은 각각 CharSequence와 String의 배열입니다.

즉, CharSequence를 볼 때마다 String 개체를 전달할 수 있습니다.


다음은 인터페이스를 구현하는 String몇 가지 구체적인 클래스 중 하나를 보여주는 다이어그램 입니다.CharSequence

특히, 스레드 안전성이 필요하지 않은 연산자를 사용하여 개체 StringBuilder를 연결하는 것보다 빠른 대안으로 일반적으로 사용됩니다 .String+

대괄호는 배열을 의미합니다. 따라서은 객체 String[]의 배열이며 String객체 일 CharSequence[]수도 있고 아닐 수도 String있지만 확실히 CharSequence인터페이스를 구현하는 객체 인 객체의 배열입니다 .

더 많은 토론은 여기 내 답변여기 내 답변을 참조하십시오 .


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.


String is the parent class that implements the interface CharSequence.

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


CharSequence represents an ordered set of characters, and defines methods for examining this character set. It is ineterface, and one implementation of this interface is String class.

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

참고URL : https://stackoverflow.com/questions/3600635/what-is-the-difference-between-charsequence-and-a-string

반응형