Using Eclipse or some other text editor, create a file named CompilerDemo.java containing the following java statements:
public class CompilerDemo { public static void main(String[] args) { int x = methodA(100,50); int y = methodB(300,125); }
// this method uses a for loop to compute the sum public static int methodA(int initialValue, int maxValue ) { int sum = initialValue; for(int i=1; i<=maxValue; i+=2 ) { sum += i; } return sum; }
// this method uses an equivalent while loop to compute the sum public static int methodB(int initialValue, int maxValue ) { int sum = initialValue; int i=1; while( i<=maxValue ) { sum += i; i+=2; } return sum; } }
Use the console-based java compiler command to compile this file into CompilerDemo.class:
javac -g -d ./bin src/CompilerDemo.java
Note: This assumes you issue the command from a location on your filesystem that contains a "src" subfolder containing CompilerDemo.java, along with another subfolder named "bin" that will contain the CompilerDemo.class file generated by the java compiler.
Next, use the console-based javap command to disassemble the binary CompilerDemo.class file containing byte-codes into human-readable mnemonics:
javap -c -l -private -s -classpath ./bin CompilerDemo > output.txt
View the resulting output.txt file in Notepad or some other text editor. Examine the byte-code mnemonics for both methodA and methodB.
How do they differ? Explain.
Submission: Add your explanation to the output.txt file containing the
disassembly, and submit the file to Blackboard.
Due date: Friday 8am. Note that no credit is given for late homework.