자바 String 바이트 코드 분석

13390 단어 java String
자바  string 면접 문제 가 많아 요...인터넷 에서 도 말 이 많아 요...
 여기 서 string 의 용법 의 바이트 코드 를 해석 해 보 겠 습 니 다...
public class C{
		public C(){

		}
	public C(String a){
		 this.str=a;
		}
	
 String str0 = "min0";	
 static	String str = "min";
 static	String str2 = "min2";
 final	String str3 = "minmin2";
 final String str4 = "min3";
 public void t(int b){
		System.out.println("min"+str2);
		System.out.println("t--"+str3);
		System.out.println(str4+str3);
		System.out.println(str0+str);
	}
	public static void main(String[] args) {
		new A().t(2);
	
	}

}

   자바 p-verbose 를 통 해 바이트 코드 의 저장 정 보 를 살 펴 보면 다음 과 같 습 니 다.
 
Compiled from "C.java"
public class C extends java.lang.Object
  SourceFile: "C.java"
  minor version: 0 //class       
  major version: 49//class       
  Constant pool://   
  
//Method:   
 //Field:   
 //String:    
 //Asciz:  Asciz   CONSTANT_Utf8 ,  C ONSTANT_Utf8             :     、          、                      。 
 //NameAndType:       
 //Class:  
  
const #1 = Method       #24.#44;        //  java/lang/Object."<init>":()V //CONSTANT_Methodref_info (10)
const #2 = String       #45;    //  min0     //           CONSTANT_String_info (8)
const #3 = Field        #23.#46;        //  C.str0:Ljava/lang/String;//  static           
const #4 = String       #47;    //  minmin2  //           CONSTANT_String_info (8)
const #5 = Field        #23.#48;        //  C.str3:Ljava/lang/String;//  static     
const #6 = String       #49;    //  min3    //          
const #7 = Field        #23.#50;        //  C.str4:Ljava/lang/String;//  static     
const #8 = Field        #23.#51;        //  C.str:Ljava/lang/String;//static     
const #9 = Field        #52.#53;        //  java/lang/System.out:Ljava/io/PrintS
tream;
const #10 = class       #54;    //  java/lang/StringBuilder          CONSTANT_Class_info
const #11 = Method      #10.#44;        //  java/lang/StringBuilder."<init>":()V//         CONSTANT_Methodref_info

const #12 = String      #55;    //  min    //          CONSTANT_String_info (8)
const #13 = Method      #10.#56;        //  java/lang/StringBuilder.append:(Ljav
a/lang/String;)Ljava/lang/StringBuilder;////         CONSTANT_Methodref_info
const #14 = Field       #23.#57;        //  C.str2:Ljava/lang/String;//static     
const #15 = Method      #10.#58;        //  java/lang/StringBuilder.toString:()L
java/lang/String;
const #16 = Method      #59.#60;        //  java/io/PrintStream.println:(Ljava/l
ang/String;)V
const #17 = String      #61;    //  t--minmin2   //          
const #18 = String      #62;    //  min3minmin2  //          
const #19 = class       #63;    //  A
const #20 = Method      #19.#44;        //  A."<init>":()V
const #21 = Method      #19.#64;        //  A.t:(I)V
const #22 = String      #65;    //  min2
const #23 = class       #66;    //  C
const #24 = class       #67;    //  java/lang/Object
const #25 = Asciz       str0;
const #26 = Asciz       Ljava/lang/String;;
const #27 = Asciz       str;
const #28 = Asciz       str2;
const #29 = Asciz       str3;
const #30 = Asciz       ConstantValue;
const #31 = Asciz       str4;
const #32 = Asciz       <init>;
const #33 = Asciz       ()V;
const #34 = Asciz       Code;
const #35 = Asciz       LineNumberTable;
const #36 = Asciz       (Ljava/lang/String;)V;
const #37 = Asciz       t;
const #38 = Asciz       (I)V;
const #39 = Asciz       main;
const #40 = Asciz       ([Ljava/lang/String;)V;
const #41 = Asciz       <clinit>;
const #42 = Asciz       SourceFile;
const #43 = Asciz       C.java;
const #44 = NameAndType #32:#33;//  "<init>":()V//          (name)    (descriptor)
const #45 = Asciz       min0;
const #46 = NameAndType #25:#26;//  str0:Ljava/lang/String;// CONSTANT_NameAndType_info
const #47 = Asciz       minmin2;
const #48 = NameAndType #29:#26;//  str3:Ljava/lang/String;
const #49 = Asciz       min3;
const #50 = NameAndType #31:#26;//  str4:Ljava/lang/String;
const #51 = NameAndType #27:#26;//  str:Ljava/lang/String;
const #52 = class       #68;    //  java/lang/System
const #53 = NameAndType #69:#70;//  out:Ljava/io/PrintStream;
const #54 = Asciz       java/lang/StringBuilder;
const #55 = Asciz       min;
const #56 = NameAndType #71:#72;//  append:(Ljava/lang/String;)Ljava/lang/String
Builder;
const #57 = NameAndType #28:#26;//  str2:Ljava/lang/String;
const #58 = NameAndType #73:#74;//  toString:()Ljava/lang/String;
const #59 = class       #75;    //  java/io/PrintStream
const #60 = NameAndType #76:#36;//  println:(Ljava/lang/String;)V
const #61 = Asciz       t--minmin2;
const #62 = Asciz       min3minmin2;
const #63 = Asciz       A;
const #64 = NameAndType #37:#38;//  t:(I)V
const #65 = Asciz       min2;
const #66 = Asciz       C;
const #67 = Asciz       java/lang/Object;
const #68 = Asciz       java/lang/System;
const #69 = Asciz       out;
const #70 = Asciz       Ljava/io/PrintStream;;
const #71 = Asciz       append;
const #72 = Asciz       (Ljava/lang/String;)Ljava/lang/StringBuilder;;
const #73 = Asciz       toString;
const #74 = Asciz       ()Ljava/lang/String;;
const #75 = Asciz       java/io/PrintStream;
const #76 = Asciz       println;

{
java.lang.String str0;

static java.lang.String str;

static java.lang.String str2;

final java.lang.String str3;
  Constant value: String minmin2
final java.lang.String str4;
  Constant value: String min3
  
//   Locals           ,    1,       Dog()        ,   0 !      
//            ,jvm        ,        、      、            
// static  ,                     (this)     ,         ,      
//           index 0       this,                    
//static       ,     this      
//Args_size      ,public C();    this  ,  value 1    
public C();
  Code:
   Stack=2, Locals=1, Args_size=1
   0:   aload_0
   1:   invokespecial   #1; //Method java/lang/Object."<init>":()V //         
   4:   aload_0
   5:   ldc     #2; //String min0
   7:   putfield        #3; //Field str0:Ljava/lang/String;
   10:  aload_0
   11:  ldc     #4; //String minmin2
   13:  putfield        #5; //Field str3:Ljava/lang/String;
   16:  aload_0
   17:  ldc     #6; //String min3
   19:  putfield        #7; //Field str4:Ljava/lang/String;
   22:  return
  LineNumberTable:
   line 6: 0
   line 14: 4
   line 17: 10
   line 18: 16
   line 8: 22

//invokespecial  //      、          
invokevirtual  //      (     、static  )      
invokestatic   //  static      
public C(java.lang.String);
  Code:
   Stack=2, Locals=2, Args_size=2
   0:   aload_0
   1:   invokespecial   #1; //Method java/lang/Object."<init>":()V //        
   4:   aload_0
   5:   ldc     #2; //String min0 //              
   7:   putfield        #3; //Field str0:Ljava/lang/String;
   10:  aload_0
   11:  ldc     #4; //String minmin2
   13:  putfield        #5; //Field str3:Ljava/lang/String;
   16:  aload_0
   17:  ldc     #6; //String min3
   19:  putfield        #7; //Field str4:Ljava/lang/String;
   22:  aload_0
   23:  pop
   24:  aload_1
   25:  putstatic       #8; //Field str:Ljava/lang/String;
   28:  return
  LineNumberTable:
   line 10: 0
   line 14: 4
   line 17: 10
   line 18: 16
   line 11: 22
   line 12: 28

public void t(int);
  Code:
   Stack=3, Locals=2, Args_size=2
   0:   getstatic       #9; //Field java/lang/System.out:Ljava/io/PrintStream;
   3:   new     #10; //class java/lang/StringBuilder  //       ,      ,         
   6:   dup   //     stack( ) 
   7:   invokespecial   #11; //Method java/lang/StringBuilder."<init>":()V //      
   10:  ldc     #12; //String min //               
   12:  invokevirtual   #13; //Method java/lang/StringBuilder.append:(Ljava/lang 
/String;)Ljava/lang/StringBuilder; //    
   15:  getstatic       #14; //Field str2:Ljava/lang/String;//  static  
   18:  invokevirtual   #13; //Method java/lang/StringBuilder.append:(Ljava/lang
/String;)Ljava/lang/StringBuilder;//    
   21:  invokevirtual   #15; //Method java/lang/StringBuilder.toString:()Ljava/l
ang/String;//    
   24:  invokevirtual   #16; //Method java/io/PrintStream.println:(Ljava/lang/St
ring;)V//    
   27:  getstatic       #9; //Field java/lang/System.out:Ljava/io/PrintStream;//  static  
   30:  ldc     #17; //String t--minmin2 //           
   32:  invokevirtual   #16; //Method java/io/PrintStream.println:(Ljava/lang/St
ring;)V//    
   35:  getstatic       #9; //Field java/lang/System.out:Ljava/io/PrintStream;
   38:  ldc     #18; //String min3minmin2  //           
   40:  invokevirtual   #16; //Method java/io/PrintStream.println:(Ljava/lang/St
ring;)V//    
   43:  getstatic       #9; //Field java/lang/System.out:Ljava/io/PrintStream;//  static  
   46:  new     #10; //class java/lang/StringBuilder //       ,      ,        
   49:  dup
   50:  invokespecial   #11; //Method java/lang/StringBuilder."<init>":()V//    
   53:  aload_0
   54:  getfield        #3; //Field str0:Ljava/lang/String;//  str0  
   57:  invokevirtual   #13; //Method java/lang/StringBuilder.append:(Ljava/lang
/String;)Ljava/lang/StringBuilder;//    
   60:  getstatic       #8; //Field str:Ljava/lang/String;
   63:  invokevirtual   #13; //Method java/lang/StringBuilder.append:(Ljava/lang
/String;)Ljava/lang/StringBuilder; //    
   66:  invokevirtual   #15; //Method java/lang/StringBuilder.toString:()Ljava/l
ang/String; //    
   69:  invokevirtual   #16; //Method java/io/PrintStream.println:(Ljava/lang/St
ring;)V //    
   72:  return
  LineNumberTable:
   line 20: 0
   line 21: 27
   line 22: 35
   line 23: 43
   line 24: 72

public static void main(java.lang.String[]);
  Code:
   Stack=2, Locals=1, Args_size=1
   0:   new     #19; //class A
   3:   dup
   4:   invokespecial   #20; //Method A."<init>":()V
   7:   iconst_2
   8:   invokevirtual   #21; //Method A.t:(I)V
   11:  return
  LineNumberTable:
   line 26: 0
   line 28: 11

static {};
  Code:
   Stack=1, Locals=0, Args_size=0
   0:   ldc     #12; //String min
   2:   putstatic       #8; //Field str:Ljava/lang/String;
   5:   ldc     #22; //String min2
   7:   putstatic       #14; //Field str2:Ljava/lang/String;
   10:  return
  LineNumberTable:
   line 15: 0
   line 16: 5

}

  바이트 코드 정 보 를 통 해 우 리 는 발견 할 수 있다.
system.out.print("min"+str2);Stringbuilder.append(String).append(I)toString;상수 탱크 1 개 대상 2 개 대상
   0:   getstatic       #9; //Field java/lang/System.out:Ljava/io/PrintStream;
   3:   new     #10; //class java/lang/StringBuilder//StringBuilder  
   6:   dup
   7:   invokespecial   #11; //Method java/lang/StringBuilder."<init>":()V
   10:  ldc     #12; //String min
   12:  invokevirtual   #13; //Method java/lang/StringBuilder.append:(Ljava/lang
/String;)Ljava/lang/StringBuilder;
   15:  getstatic       #14; //Field str2:Ljava/lang/String;
   18:  invokevirtual   #13; //Method java/lang/StringBuilder.append:(Ljava/lang
/String;)Ljava/lang/StringBuilder;
   21:  invokevirtual   #15; //Method java/lang/StringBuilder.toString:()Ljava/l
ang/String;//  stringbuilder.toString()  。    
   24:  invokevirtual   #16; //Method java/io/PrintStream.println:(Ljava/lang/St
ring;)V





 System.out.println(str0+str);
문자열 맞 춤 법...결과 가 같다
 
 
System.out.println("t--"+str3);
str 3 는 final 수식 이기 때문에 컴 파일 하 는 동안 상수 탱크 에 있 습 니 다.t-minmin 2.
const #17 = String      #61;    //  t--minmin2
 
System.out.println(str4+str3);
str4.str 3 는 모두 final 컴 파일 상수 이기 때문에 동일 합 니 다.
const #62 = Asciz       min3minmin2;
 
 
 
 System.out.println("안녕하세요"+"요");
컴 파일 기간("안녕하세요"+"요")때 문 입 니 다.이미 String 상수 탱크 에 있 습 니 다.위 와 같다
const #19 = String      #64;    //  네,안녕하세요?
 
 
시간 문제 로.비교적 조잡 하 게 썼 다.여기까지 썼 습 니 다.오후 에 출근 해 야 돼 요.잠깐 자고...다음 에 쓰 겠 습 니 다.

좋은 웹페이지 즐겨찾기