In: Computer Science
Get the file “HW4Short.java”. Compile it and look at its bytecode. [TO DO:] Write a step-by-step description of the bytecode, imitating the style of my explanation in problem 1.
Explantionin promblem1:
0 iconst_0 push constant 0 onto the stack
1 istore_1 pop stack and store in location 1 (sum = 0)
2 iconst_0 push constant 0 onto the stack
3 istore_2 pop stack and store in location 2 (i = 0)
4 iload_2 push contents of location 2 onto stack (i)
5 bipush 10 push constant 10 onto stack
7 if_icmpge 20 pop two elements and compare with ">=" (if (i >= 10)) if test is true, go to line 20
10 iload_1 [test was false:] push sum onto stack
11 iload_2 push i onto stack
12 iadd pop two elements, add, push result onto stack 1
13 istore_1 pop stack and store in sum (sum = sum + i)
14 iinc 2 by 1 increment location 2 by 1 (i=i+1)
17 goto 4 go back to line 4 and repeat the loop
20 return
I am most interested in seeing how the bytecode demonstrates “short circuit evaluation” of the boolean “&&” operator, so be careful when explaining those parts. Place the bytecode, along with your step-by-step explanation, in a text document named q2.txt, including your name at the top. Use the following format:
0 bipush 10 push the constant 10 onto the stack
2 istore_1 pop the 10 and save it in i
3 bipush 20 push 20 onto the stack
5 istore_2 ... etc. ...
6 iconst_m1
7 istore_3 ...
etc. ...
// Needed for problem 2, lab 4 | |
public class HW4Short { | |
public int f() { | |
int i = 10, j = 20, k = 0; | |
if (i > 10 && j == 20) | |
k = 100; | |
return k; | |
} | |
} |
Solution:
Bytecode:It contain set of instruction to run your java code in JVM machine.When you compiled your java code ,compiler will generate the bytecode in the form .classfile.Using this bytecode file we can run our application in any machine.It will acts as platform indepent code.
Step-by-step bytecode of HW4Short class
//bytecode use stack push,pop operation to store the constant value
//push the constant value 10 to stack and store that it in the constant i
0 bipush 10 push the constant 10 onto the stack
2 istore_1 pop the 10 and save it in i
3 bipush 20 push the constant 20 onto the stack
5 istore_2 pop the 20 and save it in j
6 iconst_0 push the constant 0 onto the stack
7istore_3 pop the 0 and svae it in k
8 iload_1 loadthe content of location 1(i=1) on to the stack
9 bipush 10 push the constant 10 onto stack
11 if_icmple 12 compare the stack top element (i is greater than 10 if not jump to the label 12--here label 12 means end of the loop)
14 iload_2 load the content of the loaction 2(j=20) on to stack
15 bipush 20 push the constant 20 onto the stack
17 if_icmpne 6 compare the the top of the stack j and 20 value (if two value are not equal jump to the label 6 k=0,if true below instrucrion will exceute)
20 bipush 100 push the constant 100 onto the stack
22 istore_3 pop the 100 and save it in k
23 iload_3 load the content of the loaction 3 (K value)on to the stack
24 ireturn return k value
Note :
Short circuit ->is used to excute the loop operation very quickly to improve the performance .From given HW$Short It using && if first part of expression failed it will exit from the loop.It is imilar to the logical AND operation.If both expresssion true it will proceed the action in the loop.