總網頁瀏覽量

2016年5月3日 星期二

[Java] ByteCode 的 String - stringtest.java

記錄

Test 1:
純粹宣告一個public的類別

string_test.java
public class string_test{ }

- - - - -
java  string_test

Error: Main method not found in class string_test, please define the main method as:
   public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application


執行錯誤,因為沒有程式進入點 main
- - - - -
javap  string_test
Classfile /c:/../string_test.class
  Last modified 2016/5/3; size 196 bytes
  MD5 checksum ..
  Compiled from "string_test.java"
public class string_test
  minor version: 0
  major version: 52
  flags: ACC_PUBLIC, ACC_SUPER
Constant pool:
   #1 = Methodref          #3.#10   // java/lang/Object."<init>":()V
   #2 = Class              #11      // string_test
   #3 = Class              #12      // java/lang/Object
   #4 = Utf8               <init>
   #5 = Utf8               ()V
   #6 = Utf8               Code
   #7 = Utf8               LineNumberTable
   #8 = Utf8               SourceFile
   #9 = Utf8               string_test.java
  #10 = NameAndType        #4:#5          // "<init>":()V
  #11 = Utf8               string_test
  #12 = Utf8               java/lang/Object
{
  public string_test();
    descriptor: ()V
    flags: ACC_PUBLIC
    Code:
      stack=1, locals=1, args_size=1
         0: aload_0
         1: invokespecial #1 // Method java/lang/Object."<init>":()V
         4: return
      LineNumberTable:
        line 1: 0
}
SourceFile: "string_test.java"

- - - - -
- - - - -

Test 2:
加入了程式進入點函式,詳細內容請查其他網站。
string_test.java

public class string_test{
    public static void main(String args[]){    }
}
- - - - -
java  string_test
沒有執行錯誤,正常執行。

- - - - -
javap  string_test
多了一個函式,函式是個有輸入參數 args,是個 String[] 字串陣列。
在 class file 中,會以函式來區分,所以可以看到多了一個區域來存放 main。

Classfile /c:/../string_test.class
  Last modified 2016/5/3; size 267 bytes
  MD5 checksum ..
  Compiled from "string_test.java"
public class string_test
  minor version: 0
  major version: 52
  flags: ACC_PUBLIC, ACC_SUPER
Constant pool:
   #1 = Methodref          #3.#12   // java/lang/Object."<init>":()V
   #2 = Class              #13      // string_test
   #3 = Class              #14      // java/lang/Object
   #4 = Utf8               <init>
   #5 = Utf8               ()V
   #6 = Utf8               Code
   #7 = Utf8               LineNumberTable
   #8 = Utf8               main
   #9 = Utf8               ([Ljava/lang/String;)V

  #10 = Utf8               SourceFile
  #11 = Utf8               string_test.java
  #12 = NameAndType        #4:#5          // "<init>":()V
  #13 = Utf8               string_test
  #14 = Utf8               java/lang/Object
{
  public string_test();
    descriptor: ()V
    flags: ACC_PUBLIC
    Code:
      stack=1, locals=1, args_size=1
         0: aload_0
         1: invokespecial #1 // Method java/lang/Object."<init>":()V
         4: return
      LineNumberTable:
        line 1: 0

  public static void main(java.lang.String[]);
    descriptor: ([Ljava/lang/String;)V
    flags: ACC_PUBLIC, ACC_STATIC
    Code:
      stack=0, locals=1, args_size=1
         0: return
      LineNumberTable:
        line 6: 0

}
SourceFile: "string_test.java" 

- - - - -
- - - - -

Test 3:
多宣告了一個字串參數,但是不給他值。

string_test.java
 
public class string_test{
    public static void main(String args[]){   String s;   }
}

- - - - -
java  string_test
沒有執行錯誤,正常執行。

- - - - -
javap  string_test
因為多宣告了一個參數,所以Local variable會多 1。
.
.
  public static void main(java.lang.String[]);
    descriptor: ([Ljava/lang/String;)V
    flags: ACC_PUBLIC, ACC_STATIC
    Code:
      stack=0, locals=2, args_size=1
         0: return
      LineNumberTable:
        line 6: 0

}
SourceFile: "string_test.java"
 

- - - - -
- - - - -


string_test.java
給字串 s 值 "s";
 
public class string_test{
    public static void main(String args[]){ String s = "Y"; }
}

- - - - -
java  string_test
沒有執行錯誤,正常執行。

- - - - -
javap  string_test
由opcode code 可以看到,現在會先由 constant pool 中將字串訊息引入,
在將他用 ASTORE 來和 local variable 位置 1 的值連結。

 LDC - Push item from constant pool.
ASTORE - Store reference into local variable  




Classfile /c:/../string_test.class
  Last modified 2016/5/3; size 281 bytes
  MD5 checksum ..
  Compiled from "string_test.java"
public class string_test
  minor version: 0
  major version: 52
  flags: ACC_PUBLIC, ACC_SUPER
Constant pool:
   #1 = Methodref          #4.#13  // java/lang/Object."<init>":()V
   #2 = String             #14     // Y
   #3 = Class              #15     // string_test
   #4 = Class              #16     // java/lang/Object
   #5 = Utf8               <init>
   #6 = Utf8               ()V
   #7 = Utf8               Code
   #8 = Utf8               LineNumberTable
   #9 = Utf8               main
  #10 = Utf8               ([Ljava/lang/String;)V
  #11 = Utf8               SourceFile
  #12 = Utf8               string_test.java
  #13 = NameAndType        #5:#6          // "<init>":()V
  #14 = Utf8               Y
  #15 = Utf8               string_test
  #16 = Utf8               java/lang/Object
{
  public string_test();
    descriptor: ()V
    flags: ACC_PUBLIC
    Code:
      stack=1, locals=1, args_size=1
         0: aload_0
         1: invokespecial #1 // Method java/lang/Object."<init>":()V
         4: return
      LineNumberTable:
        line 1: 0

  public static void main(java.lang.String[]);
    descriptor: ([Ljava/lang/String;)V
    flags: ACC_PUBLIC, ACC_STATIC
    Code:
      stack=1, locals=2, args_size=1
         0: ldc           #2     // String Y
         2: astore_1

         3: return
      LineNumberTable:
        line 3: 0
        line 6: 3

}
SourceFile: "string_test.java"

- - - - -
- - - - -

string_test.java
 
public class string_test{
    public static void main(String args[]){
        String s;
        /*String s1 = "s1";
        System.out.println(s);*/
    }
}
- - - - -
java  string_test
沒有執行錯誤,正常執行。

- - - - -
javap  string_test
多了一個函式,函式是
- - - - -
- - - - -

string_test.java
 
public class string_test{
    public static void main(String args[]){
        String s;
        /*String s1 = "s1";
        System.out.println(s);*/
    }
}
- - - - -
java  string_test
沒有執行錯誤,正常執行。

- - - - -
javap  string_test
多了一個函式,函式是
- - - - -
- - - - -

string_test.java
 
public class string_test{
    public static void main(String args[]){
        String s;
        /*String s1 = "s1";
        System.out.println(s);*/
    }
}
- - - - -
java  string_test
沒有執行錯誤,正常執行。

- - - - -
javap  string_test
多了一個函式,函式是
- - - - -
- - - - -

string_test.java
 
public class string_test{
    public static void main(String args[]){
        String s;
        /*String s1 = "s1";
        System.out.println(s);*/
    }
}
- - - - -
java  string_test
沒有執行錯誤,正常執行。

- - - - -
javap  string_test
多了一個函式,函式是



沒有留言:

張貼留言