Reverse BASICS (13) JAVA (3)
In the simple example of the 54.13 array 54.13.1, we first create an array with an integer of 10 and initialize it.
public static void main(String[] args){ int a[]=new int[10]; for (int i=0; i<10; i++) a[i]=i; dump (a);}
Public static void main (java. lang. string []); flags: ACC_PUBLIC, ACC_STATICCode: stack = 3, locals = 3, args_size = 10: bipush 102: newarray int4: astore_15: iconst_06: istore_27: iload_28: bipush 1010: if_icmpge 2313: aload_114: iload_215: iload_216: iastore17: iinc 2,120: goto 723: aload_124: invokestatic #4 // Method dump :( [Ç I) V27: returnnewarray command, an array with 10 integer elements is created. The bipush command is used to set the array size, and the result is returned to the top of the stack. Set the array type with the newarry instruction operator. After the newarray is executed, reference (pointer) to the newly created data. In the top slot of the stack, the astore_1 storage reference points to the No. 1 slot of LVA, and the second part of the main () function, is the element that stores the value 1 in a loop to the corresponding element group. Aload_1 gets the data reference and puts it into the stack. Lastore stores integer values from the heap to the prime group and references the top of the current stack. The main () function is used to represent the dump () function. The parameter is the (row offset 23) that is prepared for the aload_1 command. Now we enter the dump () function.
public static void dump(int a[]){ for (int i=0; i<a.length; i++) System.out.println(a[i]);}
Public static void dump (int []); flags: ACC_PUBLIC, ACC_STATICCode: stack = 3, locals = 2, args_size = 10: iconst_01: istore_12: iload_13: aload_04: arraylength5: if_icmpge 238: getstatic #2 // Field java/Ç çlang/System. out: Ljava/io/PrintStream; 11: aload_012: iload_113: iaload14: invokevirtual #3 // Method java/io Ç ç/ PrintStream. println :( I) V17: iinc 1,120: goto 223: return to the referenced array in the 0 slot,. the length Expression is converted to arr in the source code. Aylength command, which gets the array reference, and the array size is at the top of the stack. Iaload row offset 13 is used to load data elements. It needs to be referenced in the array in the stack. Using aload_0 11 and indexing (using iload_1 for row offset 12) is understandable. The command prefix may be incorrectly understood, just like the array command, which is incorrect, these commands work with object references. Arrays and strings are both objects. 54.13.2 an example of summation of array elements
public class ArraySum{ public static int f (int[] a) { int sum=0; for (int i=0; i<a.length; i++) sum=sum+a[i]; return sum; }}
Public static int f (int []); flags: ACC_PUBLIC, ACC_STATICCode: stack = 3, locals = 3, args_size = 10: iconst_01: istore_12: iconst_03: istore_24: iload_25: aload_06: arraylength7: if_icmpge 2210: iload_111: aload_012: iload_213: iaload14: iadd15: istore_116: iinc 2,119: goto 422: iload_123: ireturnLVA slo0 is an array reference, LVA slot 1 is a local variable and. 54.13.3 the unique data parameter of the main () function allows us to use the unique main () function parameter, a string array.
public class UseArgument{ public static void main(String[] args) { System.out.print("Hi, "); System.out.print(args[1]); System.out.println(". How are you?"); }}
934 0 parameter (argument) 0th parameters are Program (similar to C/C ++). Therefore, the first parameter is supported. Public static void main (java. lang. string []); flags: ACC_PUBLIC, ACC_STATICCode: stack = 3, locals = 1, args_size = 10: getstatic #2 // Field java/Ç lang/System. out: Ljava/io/PrintStream; 3: ldc #3 // String Hi, 5: invokevirtual #4 // Method java/io ⤦ ç/ PrintStream. print :( Ljava/lang/String;) V8: getstatic #2 // Field java/Ç çlang/System. out: Ljava/io/PrintStream; 11: aload_012: iconst_113: aaload14: invokevirtua L #4 // Method java/io ⤦ ç/ PrintStream. print :( Ljava/lang/String;) V17: getstatic #2 // Field java/Ç çlang/System. out: Ljava/io/PrintStream; 20: ldc #5 // String. how ⤦ çare you? 22: invokevirtual #6 // Method java/io ⤦ ç/ PrintStream. println :( Ljava/lang/String;) V25: returnaload_0 is loaded on 11 rows, and the reference of 0th LVA slots (unique parameter of main () function) iconst_1 and aload are offset at 12, 13, get the reference of the first element of the array (counting from 0). the reference of the string object is shifted to 14 rows at the top of the stack and is given to the println method. 54.1.34 initialize a String Array
class Month{ public static String[] months = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; public String get_month (int i) { return months[i]; };}
The get_month () function is simple and public java. lang. string get_month (int); flags: ACC_PUBLICCode: stack = 2, locals = 2, args_size = 20: getstatic #2 // Field months: [Ç Ljava/lang/String; 3: iload_14: aaload5: areturnaaload operation array reference, java string is an object, so a_instructiong is used to operate on them. areturn returns a reference to a string object. The month [] value is initialized? Static {}; flags: ACC_STATICCode: stack = 4, locals = 0, args_size = 00: bipush 122: anewarray #3 // class java/Ç çlang/String5: dup6: iconst_07: ldc #4 // String January9: aastore10: dup11: feature: ldc #5 // String ⤦ çfebruary14: aastore15: dup16: iconst_217: ldc #6/String March19: aastore20: dup21: iconst_322: ldc #7 // String April24: aastore25: dup26: iconst_427: ldc #8 // String May29: aastore 30: dup31: iconst_532: ldc #9 // String June34: aastore35: dup36: bipush 638: ldc #10 // String July40: aastore41: dup42: bipush 744: ldc #11 // String August46: aastore47: dup48: bipush 850: ldc #12 // String ⤦ çseptember52: aastore53: dup54: bipush 956: ldc #13 // String October58: aastore59: dup60: bipush 1062: ldc #14 // String ⤦ çnovember64: aastore65: dup66: bipush 1168: ldc #15 // String ⤦ ço December70: aastore71: putstatic #2 // Field months: [Ç çljava/lang/String; 74: return937 anewarray creates a reference to a new array (a is a prefix) the object type is defined in the anewarray operand, which is a "java/lang/string" text string, and the bipush 1L before this is set the size of the array. Here we can see a new instruction dup, which is a well-known computer instruction for Stack operations. Used to copy the top value of the stack. (Including the later programming language) it is used to copy the reference of the array. Because aastore Zhang Lingling plays the role of the array in the pop-up stack, but later, aastore needs to be used once, java compiler, it is best to replace the getstatic command with dup, used to generate the storage operation of each previous array. For example, the month field. 54.13.5 Variable Parameter Function, in fact, uses an array, actually uses an array.
public static void f(int... values){ for (int i=0; i<values.length; i++) System.out.println(values[i]);}public static void main(String[] args){ f (1,2,3,4,5);}
Public static void f (int ...); flags: ACC_PUBLIC, ACC_STATIC, ACC_VARARGSCode: stack = 3, locals = 2, args_size = 10: iconst_01: istore_12: iload_13: aload_04: arraylength5: if_icmpge 238: getstatic #2 // Field java/Ç çlang/System. out: Ljava/io/PrintStream; 11: aload_012: iload_113: iaload14: invokevirtual #3 // Method java/io Ç ç/ PrintStream. println :( I) V17: iinc 1,120: goto 223: returnf () function to obtain an integer Array Using aloa D_0 offsets three rows in rows. Get the size of an array, and so on. Public static void main (java. lang. string []); flags: ACC_PUBLIC, ACC_STATICCode: stack = 4, locals = 1, args_size = 10: iconst_51: newarray int3: dup4: iconst_05: Bytes: iastore7: dup8: iconst_19: iconst_210: iastore11: dup12: Generation: iconst_314: iastore15: dup16: iconst_317: Generation: iastore19: dup20: Generation: iastore23: invokestatic #4 // Method :( [I) v26: The return element group is constructed in the main () function, When the newarray command is used, f () is called after the filling is slow. 939 simply put, the array object is not destroyed in main () and is not destructed throughout java. Because the JVM garbage collection is not automatic, when it feels necessary. What is the format () method? It uses two parameters as input, a string, and an array object. Public PrintStream format (String format, Object... args formats) let's take a look.
public static void main(String[] args){ int i=123; double d=123.456; System.out.format("int: %d double: %f.%n", i, d⤦Ç );}
Public static void main (java. lang. string []); flags: ACC_PUBLIC, ACC_STATICCode: stack = 7, locals = 4, args_size = 10: bipush 1232: istore_13: ldc2_w #2/double 123.456 Ç çd6: dstore_27: getstatic #4 // Field java/Ç çlang/System. out: Ljava/io/PrintStream; 10: ldc #5 // String int: % d Ç double: % f. % n12: iconst_213: anewarray #6 // class java/Ç çlang/Object16: dup17: iconst_018: iload_119: invokestatic #7 // Method java/Ç çlang/Integer. valueOf :( I) Ljava/lang/Integer; 22: aastore23: dup24: iconst_125: dload_226: invokestatic #8 // Method java/⤦ çlang/Double. valueOf :( D) Ljava/lang/Double; 29: aastore30: invokevirtual #9 // Method java/io Ç ç/ PrintStream. format :( Ljava/lang/String; [Ljava/lang/Object Ç;) Ljava/io/PrintStream; 33: pop34: return so the int and double types are the integer and double objects first, which are used as the value of the method... The format () method requires that the object Lei Xiang's object be used as the input, because the integer and double classes inherit from the root class root. They are suitable for elements input as arrays. On the other hand, arrays are always homogeneous. For example, the same Array cannot contain two different data types. Data of the integer and double types cannot be put into arrays at the same time. The object of the array object is in line 13, and the integer object is added to the row offset 22. double Object is added to the array in line 29. The last-to-last pop command discards the elements at the top of the stack. Therefore, these return operations make the stack empty (parallel) 54.13.6 the two-dimensional array is an array in java to reference another array. Let's create a two-dimensional prime group. ()
public static void main(String[] args){ int[][] a = new int[5][10]; a[1][2]=3;}
Public static void main (java. lang. string []); flags: ACC_PUBLIC, ACC_STATICCode: stack = 3, locals = 2, args_size = 10: iconst_51: bipush 103: multianewarray #2, 2 // class "[[I" 7: astore_18: aload_19: iconst_110: aaload11: iconst_212: iconst_313: iastore14: return it creates and uses the multianewarry command: the object type and dimension are used as the operands. The array size (10*5) is returned to the stack. (Using the iconst_5 and bipush commands) Row reference is to use the iconst_1 and aaload commands to use the iconst_2 command to reference the row Reference. Is it worthwhile to write and set it to 12 rows, iastore to 13 rows, and write data elements?
public static int get12 (int[][] in){ return in[1][2];}
Public static int get12 (int [] []); flags: ACC_PUBLIC, ACC_STATICCode: stack = 2, locals = 1, args_size = 10: aload_01: iconst_12: aaload3: iconst_24: iaload5: the ireturn reference array is loaded in Row 2, and the column is set to load the array in Row 3 and iaload. 54.13.7 the 3D array is a reference to a one-dimensional array.
public static void main(String[] args){ int[][][] a = new int[5][10][15]; a[1][2][3]=4; get_elem(a);}
Public static void main (java. lang. string []); flags: ACC_PUBLIC, ACC_STATICCode: stack = 3, locals = 2, args_size = 10: iconst_51: bipush 103: bipush 155: multianewarray #2, 3 // class "[[I" 9: astore_110: aload_111: iconst_112: aaload13: iconst_214: aaload15: iconst_316: iconst_417: iastore18: aload_119: invokestatic #3 // Method ⤦ çget_elem :( [[I) I22: pop23: return it uses two aaload commands to find the right reference.
public static int get_elem (int[][][] a){ return a[1][2][3];}
Public static int get_elem (int [] [] []); flags: ACC_PUBLIC, ACC_STATICCode: stack = 2, locals = 1, args_size = 10: aload_01: iconst_12: aaload3: iconst_24: aaload5: iconst_36: iaload7: ireturn53.13.8 can stack overflow occur in java? Impossible. The length of an array actually represents the number of objects. The boundary of an array is controllable, and an exception is thrown when cross-border access occurs. 54.14 string 54.14.1 the first example string is also an object, which is constructed in the same way as other objects. (There are arrays)
public static void main(String[] args){ System.out.println("What is your name?"); String input = System.console().readLine(); System.out.println("Hello, "+input);}
Public static void main (java. lang. string []); flags: ACC_PUBLIC, ACC_STATICCode: stack = 3, locals = 2, args_size = 10: getstatic #2 // Field java/Ç lang/System. out: Ljava/io/PrintStream; 3: ldc #3 // String What is your name? 5: invokevirtual #4 // Method java/io ⤦ ç/ PrintStream. println :( Ljava/lang/String;) V8: invokestatic #5 // Method java/Ç çlang/System. console :() Ljava/io/Console; 11: invokevirtual #6 // Method java/io Ç/Console. readLine :() Ljava/lang/String; 14: astore_115: getstatic #2 // Field java/Ç çlang/System. out: Ljava/io/PrintStream; 18: new #7 // class java/Ç çlang/StringBuilder21: dup22: invokespecial #8 // Meth Od java/Ç çlang/StringBuilder. "<init>" :() V25: ldc #9 // String Hello, 27: invokevirtual #10 // Method java/Ç çlang/StringBuilder. append :( Ljava/lang/String;) Ljava/lang/Ç çstringbuilder; 30: aload_131: invokevirtual #10 // Method java/Ç çlang/StringBuilder. append :( Ljava/lang/String;) Ljava/lang/Ç çstringbuilder; 34: invokevirtual #11 // Method java/Ç çlang/StringBuilder. toString :() Ljava/lang/String; 37: Invokevirtual #4 // Method java/io ⤦ ç/ PrintStream. println :( Ljava/lang/String;) V40: return944 the readline () method is called at line 11 offset, and String reference (provided by the user) is stored at the top of the stack, in line 14 offset, string reference is stored in slot 1 of LVA. The string you entered is reloaded at the offset of 30 rows and linked to the "hello" character. The StringBulder class is used and the string constructed at the offset of 17 rows is printed by the pirntln method. 54.14.2 Example 2 Another example
public class strings{ public static char test (String a) { return a.charAt(3); }; public static String concat (String a, String b) { return a+b; }}
Public static char test (java. lang. string); flags: ACC_PUBLIC, ACC_STATICCode: stack = 2, locals = 1, args_size = 10: aload_01: iconst_32: invokevirtual #2 // Method java/Ç lang/String. charAt :( I) C5: the link to the ireturn945 string is completed using the StringBuilder class.
public static java.lang.String concat(java.lang.String, java.⤦Ç lang.String);flags: ACC_PUBLIC, ACC_STATICCode:stack=2, locals=2, args_size=20: new #3 // class java/⤦Ç lang/StringBuilder3: dup4: invokespecial #4 // Method java/⤦Ç lang/StringBuilder."<init>":()V7: aload_08: invokevirtual #5 // Method java/⤦Ç lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/⤦Ç StringBuilder;11: aload_112: invokevirtual #5 // Method java/⤦Ç lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/⤦Ç StringBuilder;15: invokevirtual #6 // Method java/⤦Ç lang/StringBuilder.toString:()Ljava/lang/String;18: areturn
Another example
public static void main(String[] args){String s="Hello!";int n=123;System.out.println("s=" + s + " n=" + n);}
String construction uses the StringBuilder class and its addition method. The constructed string is passed to the println method.
public static void main(java.lang.String[]);flags: ACC_PUBLIC, ACC_STATICCode:stack=3, locals=3, args_size=10: ldc #2 // String Hello!2: astore_13: bipush 1235: istore_26: getstatic #3 // Field java/⤦Ç lang/System.out:Ljava/io/PrintStream;9: new #4 // class java/⤦Ç lang/StringBuilder12: dup13: invokespecial #5 // Method java/⤦Ç lang/StringBuilder."<init>":()V16: ldc #6 // String s=18: invokevirtual #7 // Method java/⤦Ç lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/⤦Ç StringBuilder;21: aload_122: invokevirtual #7 // Method java/⤦Ç lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/⤦Ç StringBuilder;25: ldc #8 // String n=27: invokevirtual #7 // Method java/⤦Ç lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/⤦Ç StringBuilder;30: iload_231: invokevirtual #9 // Method java/⤦Ç lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;34: invokevirtual #10 // Method java/⤦Ç lang/StringBuilder.toString:()Ljava/lang/String;37: invokevirtual #11 // Method java/io⤦Ç /PrintStream.println:(Ljava/lang/String;)V40: return