본문 바로가기
> JAVA/Class 클래스

[Java] 2019.09- [Class] StringBuilder

by bky373 2020. 9. 20.

 

StringBuilder

 

언젠가 정리해둬야지... 계속 벼르고 있던 클래스이다.

누가 이런 내맘을 알아준 건지 [SWEA에서 1989번 초심자의 회문 검사] 문제를 푸는데 StringBulider를 사용할 필요를 느꼈다. 어차피 다음에 공부할 거 이참에 미루지 말고 StringBuilder에 대해 빠삭히 알아보기로 결심했다!

급하다면 아래 요약 부분을 참고하자!

 

클래스 생성자, 메소드 등에 대한 설명은 뒤로 하고(먼훗날.. 필요에 따라 작성할 예정)

우선 StringBuilder 클래스가 본질적으로 가지고 있는 속성이나 특징 등을 살펴보려 한다.

 


 

public final class StringBuilder

extendsObjectimplementsSerializable,Comparable<StringBuilder>,CharSequence

 

- A mutable sequence of characters. This class provides an API compatible with StringBuffer, but with no guarantee of synchronization.

> 문자들의 시퀀스이며 수정 가능하다. 이 클래스는 StringBuffer와 호환가능한 API를 제공하지만 동기화는 보장하지 않는다.

- This class is designed for use as a drop-in replacement for StringBuffer in places where the string buffer was being used by a single thread (as is generally the case).

> 이 클래스는 스트링버퍼가 (일반적으로)단일스레드에 의해 사용되는 경우, StringBuffer를 대신하기 위해 고안되었다.

- Where possible, it is recommended that this class be used in preference to StringBuffer as it will be faster under most implementations.

> 가능하면, StringBuffer보다는 StringBulider클래스 쓰는 것을 추천한다. 대부분 이 클래스의 실행 속도가 더 빠르기 때문이다.

- The principal operations on a StringBuilder are the append and insert methods, which are overloaded so as to accept data of any type.

> StringBuilder를 두고하는 주된 활동이라 하면, 이미 오버로드된 메소드들을 덧붙이고 삽입하는 것이다. 이 메소드들은 어떤 유형의 데이터이든지 접근할 수 있도록 오버로드돼있는 것들이다.

- Each effectively converts a given datum to a string and then appends or inserts the characters of that string to the string builder. The append method always adds these characters at the end of the builder; the insert method adds the characters at a specified point.

> 각각의 것들은 입력된 자료들을 문자열로 변환하고, 그 문자열의 문자들을 스트링빌더에 덧붙이거나 삽입한다. append 메소드는 항상 빌더 마지막에 문자들을 덧붙이고, insert 메소드는 명시된 특정지점에 문자열들을 더한다(끼운다).

- For example, if z refers to a string builder object whose current contents are "start", then the method call z.append("le") would cause the string builder to contain "startle", whereas z.insert(4, "le") would alter the string builder to contain "starlet".

> 예를 들어, "start"를 내용으로 하는 스트링빌더 객체 z가 있다고 하자. z.append("le")를 호출하면 스트링빌더는 "startle"가 될 것이고, 반면 z.insert(4, "le")를 호출하면 스트링빌더는 "starlet"이 될 것이다.

- In general, if sb refers to an instance of a StringBuilder, then sb.append(x) has the same effect as sb.insert(sb.length(), x).

> 일반적으로, 스트링빌더 객체 sb가 있다면, sb.append(x)는 sb.insert(sb.length(),x)한 것과 같다.

- Every string builder has a capacity. As long as the length of the character sequence contained in the string builder does not exceed the capacity, it is not necessary to allocate a new internal buffer.

> 모든 스트링빌더는 수용량을 갖고 있다. 스트링빌더 안에 있는 문자 시퀀스라면 그 길이가 한계를 초과하지 않고, 필요한 경우가 아니라면 굳이 내부 버퍼를 새로 할당할 필요가 없다.

- If the internal buffer overflows, it is automatically made larger.

> 만약 내부 버퍼가 오버플로되면(넘쳐흐르면) 자동으로 알아서 커진다.

- Instances of StringBuilder are not safe for use by multiple threads. If such synchronization is required then it is recommended thatStringBufferbe used.

> 스트링빌더 객체들은 다중스레드에선 안전하지 않다. 만약 그런 동기화가 필요하다면 StringBuffer가 낫다.

- Unless otherwise noted, passing a null argument to a constructor or method in this class will cause aNullPointerExceptionto be thrown.

> 별다른 언급이 없는 한, 생성자 또는 메소드에 null인수를 전달하면NullPointerException이 발생할 것이다.

 

API Note:

- StringBuilder implements Comparable but does not overrideequals. Thus, the natural ordering of StringBuilder is inconsistent with equals.

> StringBuilder는 Comparable를 구현하지만 equals를 오버라이드하지는 않는다. 둘 간의 nuatural ordering(자연적 질서?)이 다르기 때문이다.

- Care should be exercised if StringBuilder objects are used as keys in a SortedMap or elements in a SortedSet. SeeComparable,SortedMap, orSortedSetfor more information.

>마지막으로 StringBuilder객체를 SortedMap의 key나 SortedSet의 element로 사용한다면 각별히 주의하자.

 

Since:

1.5

 

See Also:

StringBuffer,String,Serialized Form

 



 

 

*** 요약 ***

 

StringBuilder는 문자들의 연결체를 말한다. 곧

문자들을 연결해놓은 클래스를 말하며

append, insert 등 *메소드를 통해 수정이 가능하다.

또한, 주로 단일스레드에서 사용되고

StringBuffer에 비해 빠르다는 장점이 있으나,

다중스레드에선 터진다는 문제도 안고 있다.

 

*StringBulider내 메소드는 반환타입이 StringBuilder인 경우가 많다.

String으로 사용하고자 할 때 메소드 뒤에 ".toString()"까지 붙여주는 것 잊지말자!!

 


 

 

참고 : StringBuffer에 대해

https://blog.naver.com/bo373/221647435423

 


출처 : https://docs.oracle.com/en/java/javase/12/docs/api/

 

'> JAVA > Class 클래스' 카테고리의 다른 글

[Java] 2019.09- [Class] BufferedReader  (0) 2020.09.22
[Java] 2019.09- [Class] ArrayList<E>  (0) 2020.09.20
[Java] 2019.09- [Class] StringBuffer  (0) 2020.09.20

댓글