Assembly Language for Beginners

(nextflipdebug2) #1

4.1. JAVA


The string the user entered is reloaded at offset 30 and concatenated with the “Hello, ” string using the
StringBuilderclass.


The constructed string is then printed usingprintlnat offset 37.


Second example


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_STATIC
Code:
stack=2, locals=1, args_size=1
0: aload_0
1: iconst_3
2: invokevirtual #2 // Method java/lang/String.charAt:(I)C
5: ireturn

The string concatenation is performed usingStringBuilder:


public static java.lang.String concat(java.lang.String, java.lang.String);
flags: ACC_PUBLIC, ACC_STATIC
Code:
stack=2, locals=2, args_size=2
0: new #3 // class java/lang/StringBuilder
3: dup
4: invokespecial #4 // Method java/lang/StringBuilder."<init>":()V
7: aload_0
8: invokevirtual #5 // Method java/lang/StringBuilder.append:(Ljava/lang/String⤦
Ç;)Ljava/lang/StringBuilder;
11: aload_1
12: 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);
}

And again, the strings are constructed using theStringBuilderclass and itsappendmethod, then the
constructed string is passed toprintln:


public static void main(java.lang.String[]);
flags: ACC_PUBLIC, ACC_STATIC
Code:
stack=3, locals=3, args_size=1
0: ldc #2 // String Hello!
2: astore_1
Free download pdf