Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
School of Informatics, University of Edinburgh Computer Science 1 Bh
CS1Bh Question Sheet 2
Java Byte Code
The following questions are designed to promote understanding of the material in
CS1Bh. Your tutor may discuss some of the questions, and some may be set as
homework. Work on these questions is not formally assessed, so you may discuss
answers with your fellow students (remember that this does not apply to practical
exercises). When problems on this sheet are set as homework, there is no reason to
copy solutions from somebody else; you should think about the questions yourself,
so you can discuss answers at a tutorial. If you have difficulty, you may ask a lab
demonstrator for help or consult the solution sheet on the web, when it is issued.
1. Consider the following Java method, foo(). What sequence of Java byte code
instructions would this method produce when compiled? 
1 int foo() {
2 int i = 3;
3 int j = 4;
4 int k = i * (j + j);
5 return k;
6 }
 	
2. Consider the following Java method, gcd(). Given that the remainder operator %
generates an irem instruction, what sequence of Java byte code instructions would this
method produce when compiled? 
1 static int gcd(int x, int y){
2 if (y == 0)
3 return x;
4 else
5 return gcd(y, x % y);
6 }
 	
1
School of Informatics, University of Edinburgh Computer Science 1 Bh
3. Consider the following Java byte code method, baz(). What Java source code
method would have produced this sequence of byte code instructions when compiled?
Method int baz(int, int)
0 goto 10
3 iload 2
4 iload 1
5 iadd
6 istore 2
7 iinc 1 –1
10 iload 1
11 ifne 3
14 iload 2
15 ireturn
4. Consider the following Java byte code method, bar(). What Java source code
method would have produced this sequence of byte code instructions when compiled?
Method int bar(int, int)
0 iload 1
1 ifne 6
4 iconst 0
5 ireturn
6 iload 2
7 ifne 12
10 iconst 1
11 ireturn
12 iconst 2
13 ireturn
5. Given the following Java methods, cake(), bun() and flan(), how would the Java
byte code for bun() and flan() differ? [Hint: Java byte code has a pop instruction to
pop the operand stack.]
 
1 static int cake() {
2 return 4;
3 }
4 static int bun() {
5 cake();
6 return cake();
7 }
8 static int flan() {
9 return cake() + cake();
10 }
 	
2
School of Informatics, University of Edinburgh Computer Science 1 Bh
6. The following Java byte code method was written by an inexperienced Java byte
code programmer. How could it be improved?
Method int myFirstMethod()
0 iconst 0
1 istore 1
2 iconst 1
3 istore 1
4 iload 1
5 iconst 1
6 if icmpne 11
7 goto 9
8 goto 0
9 iconst 2
10 istore 1
11 iload 1
12 ireturn
7. Consider a Java virtual machine which has the following values in memory and on
the operand stack.
1 2
memory 5 4
13
stack 3
Giving this starting state, show the intermediate states of the memory and the stack
as each instruction is executed.
1 istore 1
2 iconst 5
3 iload 1
4 iinc 1 1
5 istore 2
8. Java compilers accept command line arguments which allow you to give instruc-
tions to the compiler in order to direct the way in which Java programs are compiled.
One of these is the flag -verbose which instructs the compiler to generate messages
about what the compiler is doing.
Compile any of your own Java programs with javac -verbose instead of javac.
Read the messages which are produced by the compiler and try to work out which
parts of your program have generated which of the compiler messages.
9. The Java Virtual Machine also has an option to produce verbose output, in order
to help you understand how your program is being executed.
Execute any of your own Java programs with java -verbose instead of java. Read
the messages which are produced by the compiler and try to work out which parts of
your program have generated which of the interpreter messages.
3
School of Informatics, University of Edinburgh Computer Science 1 Bh
10. The Java programming language is compiled to Java byte code by a Java compiler.
Other languages can be compiled to Java byte code also. For example, Standard ML
can be compiled to Java byte code using the MLj compiler. Using the Internet for
reference materials, and your favourite Internet search engine, discover whether the
Artificial Intelligence languages Scheme and Prolog can be compiled to Java byte code
or not.
Questions written by Stephen Gilmore.
David Aspinall, 27th February 2003.
4