誰かが次のことを確認できますか
与えられたオプション 1
import java.text.ParseException;
import java.util.StringTokenizer;
public class stringtok
{
public static void main(String[] argv)
throws Exception
{
String data="ABC";
final StringTokenizer stoken=new StringTokenizer(data.toString(),";");
if (stoken.hasMoreTokens()) {
final String test=stoken.nextToken();
} else {
throw new ParseException("Some msg",0);
}
}
}
バイトコードで生成
Compiled from "stringtok.java"
public class stringtok {
public stringtok();
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
public static void main(java.lang.String[]) throws java.lang.Exception;
Code:
0: ldc #2 // String ABC
2: astore_1
3: new #3 // class java/util/StringTokenizer
6: dup
7: aload_1
8: invokevirtual #4 // Method java/lang/String.toString:()Ljava/lang/String;
11: ldc #5 // String ;
13: invokespecial #6 // Method java/util/StringTokenizer."<init>":(Ljava/lang/String;Ljava/lang/String;)V
16: astore_2
17: aload_2
18: invokevirtual #7 // Method java/util/StringTokenizer.hasMoreTokens:()Z
21: ifeq 32
24: aload_2
25: invokevirtual #8 // Method java/util/StringTokenizer.nextToken:()Ljava/lang/String;
28: astore_3
29: goto 43
32: new #9 // class java/text/ParseException
35: dup
36: ldc #10 // String Some msg
38: iconst_0
39: invokespecial #11 // Method java/text/ParseException."<init>":(Ljava/lang/String;I)V
42: athrow
43: return
}
オプション 2 と 3 を指定 (中括弧は同一のバイトコード)
import java.text.ParseException;
import java.util.StringTokenizer;
public class stringtok2
{
public static void main(String[] argv)
throws Exception
{
String data="ABC";
final StringTokenizer stoken=new StringTokenizer(data.toString(),";");
if (!stoken.hasMoreTokens()) throw new ParseException("Some msg",0);
final String test=stoken.nextToken();
}
}
バイトコードで生成
Compiled from "stringtok2.java"
public class stringtok2 {
public stringtok2();
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
public static void main(java.lang.String[]) throws java.lang.Exception;
Code:
0: ldc #2 // String ABC
2: astore_1
3: new #3 // class java/util/StringTokenizer
6: dup
7: aload_1
8: invokevirtual #4 // Method java/lang/String.toString:()Ljava/lang/String;
11: ldc #5 // String ;
13: invokespecial #6 // Method java/util/StringTokenizer."<init>":(Ljava/lang/String;Ljava/lang/String;)V
16: astore_2
17: aload_2
18: invokevirtual #7 // Method java/util/StringTokenizer.hasMoreTokens:()Z
21: ifne 35
24: new #8 // class java/text/ParseException
27: dup
28: ldc #9 // String Some msg
30: iconst_0
31: invokespecial #10 // Method java/text/ParseException."<init>":(Ljava/lang/String;I)V
34: athrow
35: aload_2
36: invokevirtual #11 // Method java/util/StringTokenizer.nextToken:()Ljava/lang/String;
39: astore_3
40: return
}
したがって、理論的にはバイトコード命令が少ないため、答えはオプション 2 と 3 です。
誰か確認できますか