JVM optimized - bytecode View

 1. Check the class file byte code command by javap content

  Write a simple 1.1 Test class:

public static void main(String[] args) { 
  int a = 2;
  int b = 5;
  int c = b ‐ a;
  System.out.println(c);
}

  After the class the next target directory will generate 1.2 to run a class file

         

 

 

 

  1.3 into the target directory to start cmd command window

 

 

 

  

 

 

 

  1.4 bytecode view the contents of the class file by command javap 

 

 

 

  1.5 View content generated javapTest.txt file is as follows

 

 

 

   SUMMARY roughly divided into four parts:
    First part: shows a generation source of this class java file, version information, generation time.
    Part II: shows the class constant pool involved, a total of 35 constants.
    Part III: displaying the class constructor, the compiler automatically inserted.
    Part IV: displays information about the main parties. (This is the need to focus our attention) 
 
   2. constant pool 
 

 

 

 

   2.1 Method descriptor 

Example:

  The method descriptor for the method: 

Object m(int i, double d, Thread t) {...}

  is

(IDLjava/lang/Thread;)Ljava/lang/Object;

 

  Method bytecode interpretation 2.2

 

 

  2.3 illustrates

 

 

 

 

  2.5 Different i ++ and ++ i in

  2.5.1 write code

 public static void main(String[] args) {
        new Test01().method1();
        new Test01().method2();
    }
    public void method1(){
        int i=1;
        int a=i++;
        System.out.println(a);
    }
    public void method2(){
        int i=1;
        int a=++i;
        System.out.println(a);
    }

 

  2.5.2 After running the class target will generate class class class and find this directory

 

 

  2.5.3 Input cmd from the command line in FIG open directory window and enter the command

 

 

  2.5.4 View Test01.txt content

 

 

  2.5.6 Comparison

i++:

  

 

 ++i:

  

  the difference:

    i++

      Only in a local variable of the figure is the sum, not pushing data into the operand stack

      The number 1 in front get, get again from the stack operation, is pressed into the local variables 

    ++i

      The digital local variables did added, and pushing data into the operand stack

      The operation of the data stack, again pressed into local variables 

 

  3. string concatenation

   String concatenation in the development process is very frequent, commonly used in three ways: 
      + No splicing: str + "456"
      StringBuilder splicing
      StringBuffer splicing 
 
  StringBuffer is thread-safe, the efficiency is relatively low, we use more of a scene is not related to
Thread safety problems, so more time will choose StringBuilder, efficiency will be higher
 
  So, the question is, StringBuilder and "+" sign stitching, which is high efficient? Next we explore through bytecode
  
  3.1 write test code
 public static void main(String[] args) {
        new Test02().m1(); 
        new Test02().m2(); 
    }
    public void m1(){ 
        String s1 = "123"; 
        String s2 = "456";
        String s3 = s1 + s2; 
        System.out.println(s3);
    }
    
    public void m2(){ 
        String s1 = "123";
        String s2 = "456"; 
        StringBuilder sb = new StringBuilder(); 
        sb.append(s1); sb.append(s2);
        String s3 = sb.toString(); System.out.println(s3); 
    }

  

  After generating the target class file generated after running 3.2 class bytecode files command (which is abbreviated as detailed in the above step operation)

 

As can be seen from the de-byte code, M1 () method is to use source code + number splice, but also in the bytecode is compiled into StringBuilder manner.
Therefore, it can be concluded, string concatenation, + and StringBuilder are equal, the same efficiency. 

Guess you like

Origin www.cnblogs.com/szhhhh/p/12463021.html