Assembly Language for Beginners

(nextflipdebug2) #1

4.1. JAVA


3: bipush 123
5: istore_2
6: getstatic #3 // Field java/lang/System.out:Ljava/io/PrintStream;
9: new #4 // class java/lang/StringBuilder
12: dup
13: invokespecial #5 // Method java/lang/StringBuilder."<init>":()V
16: ldc #6 // String s=
18: invokevirtual #7 // Method java/lang/StringBuilder.append:(Ljava/lang/String⤦
Ç;)Ljava/lang/StringBuilder;
21: aload_1
22: 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_2
31: 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;)V
40: return

4.1.15 Exceptions.


Let’s rework ourMonthexample (4.1.13 on page 681) a bit:


Listing 4.10: IncorrectMonthException.java

public class IncorrectMonthException extends Exception
{
private int index;


public IncorrectMonthException(int index)
{
this.index = index;
}
public int getIndex()
{
return index;
}
}


Listing 4.11: Month2.java

class Month2
{
public static String[] months =
{
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
};


public static String get_month (int i) throws IncorrectMonthException
{
if (i<0 || i>11)
throw new IncorrectMonthException(i);
return months[i];
Free download pdf