*. 테스트 소스
class aa {
public static void main(String[] args) {
String aa = "aa \n"
+ "bb \n"
+ "cc \n";
System.out.println(aa);
StringBuffer bb = new StringBuffer();
bb.append(" aa \n");
bb.append(" bb \n");
bb.append(" xx \n");
System.out.println(bb.toString());
StringBuffer cc = new StringBuffer()
.append(" aaa \n")
.append(" bbb \n")
.append(" ccc \n");
System.out.println(cc.toString());
}
}
*. 디어셈블 결과
C:\j2sdk1.4.2_12\bin>javap -c aa
Compiled from "aa.java"
class aa extends java.lang.Object{
aa();
Code:
0: aload_0
1: invokespecial #1; //Method java/lang/Object."<init>":()V
4: return
public static void main(java.lang.String[]);
Code:
0: ldc #2; //String aa \nbb \ncc \n
2: astore_1
3: getstatic #3; //Field java/lang/System.out:Ljava/io/PrintStream;
6: aload_1
7: invokevirtual #4; //Method java/io/PrintStream.println:(Ljava/lang/Str
ing;)V
10: new #5; //class StringBuffer
13: dup
14: invokespecial #6; //Method java/lang/StringBuffer."<init>":()V
17: astore_2
18: aload_2
19: ldc #7; //String aa \n
21: invokevirtual #8; //Method java/lang/StringBuffer.append:(Ljava/lang/S
tring;)Ljava/lang/StringBuffer;
24: pop
25: aload_2
26: ldc #9; //String bb \n
28: invokevirtual #8; //Method java/lang/StringBuffer.append:(Ljava/lang/S
tring;)Ljava/lang/StringBuffer;
31: pop
32: aload_2
33: ldc #10; //String xx \n
35: invokevirtual #8; //Method java/lang/StringBuffer.append:(Ljava/lang/S
tring;)Ljava/lang/StringBuffer;
38: pop
39: getstatic #3; //Field java/lang/System.out:Ljava/io/PrintStream;
42: aload_2
43: invokevirtual #11; //Method java/lang/StringBuffer.toString:()Ljava/la
ng/String;
46: invokevirtual #4; //Method java/io/PrintStream.println:(Ljava/lang/Str
ing;)V
49: new #5; //class StringBuffer
52: dup
53: invokespecial #6; //Method java/lang/StringBuffer."<init>":()V
56: ldc #12; //String aaa \n
58: invokevirtual #8; //Method java/lang/StringBuffer.append:(Ljava/lang/S
tring;)Ljava/lang/StringBuffer;
61: ldc #13; //String bbb \n
63: invokevirtual #8; //Method java/lang/StringBuffer.append:(Ljava/lang/S
tring;)Ljava/lang/StringBuffer;
66: ldc #14; //String ccc \n
68: invokevirtual #8; //Method java/lang/StringBuffer.append:(Ljava/lang/S
tring;)Ljava/lang/StringBuffer;
71: astore_3
72: getstatic #3; //Field java/lang/System.out:Ljava/io/PrintStream;
75: aload_3
76: invokevirtual #11; //Method java/lang/StringBuffer.toString:()Ljava/la
ng/String;
79: invokevirtual #4; //Method java/io/PrintStream.println:(Ljava/lang/Str
ing;)V
82: return
}
위 결과에서 볼 수 있듯이 StringBuffer보다 String이 속도가 더 빠르다는 것을 알 수 있다.
뭐가 좋고 나쁨을 말할순 없지만 상황에따라 잘 사용 해야 할것 같다.
참고.
1. String aa = "aa \n"
+ "bb \n"
+ "cc \n";
2. String aa = "aa \n";
aa += "bb \n";
aa += "cc \n";
1번과 2번의 결과는 다르다는 것을 알고 있어야 한다.
2번 같은 경우는 StringBuffer와 같이 서로 다른 라인으로 인식하여 실행되기 때문이다.
'Java' 카테고리의 다른 글
[Java] 자료구조!! (0) | 2010.06.03 |
---|---|
[Java] 자바 소스코드 많은 추천 싸이트!!! (0) | 2010.06.03 |
[Java] getMetaData by table (0) | 2010.01.25 |
[Java] GIF 이미지 생성 (0) | 2009.12.22 |
[Java] 원격디버깅 (0) | 2009.11.25 |