JVM & tools

Exercise 1

Using NetBeans write a simple Java program that asks the user three strings, sorts them using the method Arrays.sort and prints the ones whose length is even.

  • Goal: Checking that the NetBeans installation works; starting simple Java coding.
  • Expected output: Simple Java class working as described.

Exercise 2

Check the differences between files StrangeOne.java and StrangeTwo.java. Compile them, disassemble the obtained classes using javap -c and inspect the byte-code of the method sum. Is the byte-code the same? Can you explain why?

  • Goal: Using javap for disassembling Java code; inspecting simple bytecodes.
  • Expected output: One short sentence answering the two questions.

Exercise 3

Run the program WrongQueue and inspect its behaviour using visualvm. Can you explain the continuos growth of the heap? Find the code causing the bug and fix it.

  • Goal: Using visualvm to inspect the memory consumed by a Java program; Reviewing Java code to detect non-trivial errors; Fixing bugs
  • Expected output: One sentence identifying the bug in the code; A revised version of the class with minimal changes to fix the bug.

Exercise 4

  • As we have seen last week, the JVM has two instructions that can be used to compile a switch statement: tableswitch and lookupswitch. Taking inspiration from the examples seen at lesson, try to determine when your Java compiler uses tableswitch and when it uses lookupswitch. Does this depend only on the distance between the smallest and the largest constants in the case clauses? Are other criteria considered as well?
  • Write a simple switch which uses a String as guard. Compile it and inspect the disassembled bytecode. Try to describe informally the logics of the bytecode, and how does it implements the switch in the source code.
  • Goal: Reading disassembled bytecode to infer the compilation scheme for simple but non-trivial high level control structures,
  • Expected output: The answers to the two questions above, in English and possibly with snapshot of Java code and/or bytecode as illustration.

Author: Andrea Corradini

Created: 2021-09-30 Thu 02:21

Validate