Simple calculations
No. 5
Working with variables
Q: |
This exercise is about two variables
|
||||||||
A: |
A naive solution reads:
This unfortunately does not work. In ordinary math
e.g.
As we shall see in the section called “Operators and expressions” each of these constituents evaluates to a string thus producing: -4+100=-4100 A better solution thus forcing an arithmetic
“+” operation requires a pair of braces grouping
Reverting to the original variable values 4 and 7 still yields a correct output:
|
No. 6
Code equivalence
Q: |
The solution of Working with variables
claims full source code equivalence between |
||||
A: |
We start from the following two source code versions:
Source code equivalence means equivalence of executing the generated bytecode. The easiest case happens if compiling both versions yields identical bytecode. This is indeed true and can be proven by:
The diff command returning no result states the the two files to be identical. Otherwise we would see e.g.: diff HelloWorld.class HelloWorld.2.class
Binary files HelloWorld.class and HelloWorld.2.class differ |
No. 7
Different byte code, same execution results
Q: |
Can you imagine two different byte code files being fully equivalent with respect to execution? |
||||
A: |
There are various scenario for this to happen:
|