Explaining how Java constructs are executed or evaluated
Introduction
Sometimes, it is easy to write code without thoroughly understanding what it does ---your program worked, but you're not completely sure why. Other times, you might "know" a fact without understanding its implications, leading to misunderstandings and poor programming.
This page is designed to increase understanding and remove misunderstandings. It highlights important concepts and allows you to test yourself to make sure you avoid common misconceptions.
There are two reasons for taking this material seriously. The main reason is that learning this material properly will help you master Java and data structures and increase your programming skill. The second reason is that much of this material is guaranteed to be on prelims and the final.
Here is a way to study some of this material, be it a definition, an algorithm, a concept, or evaluation/execution of some Java code. It doesn't take long to do this.
Practice 1. Get out a blank sheet of paper and write it to the best of your ability. Compare what you wrote to our definition, algorithm, concept, or evaluation/execution. Note any differences. Next day, do the same thing. Continue doing this until you get it right.
Execution versus evaluation
An expresssion like x+2 is evaluated. If variable x contains value 4, evaluation of x+2 yields the value 6. A computer could evaluate that expression, but you could too. A function call like max(5, 3) is an expression, and evaluating it yields the value 5.
A statement, on the other hand, is not evaluated; it is executed. For example, the statements x= x+2; and (in a recipe) "Stir the batter until smooth" are executed, or carried out by someone or something ---a person, a computer, a chef.
The Java statement System.out.println(6); is a call on procedure println. Executing the call causes 6 to be printed somewhere; it does not "return" a result. Function calls, which are expressions, are different from procedure calls, which are statements.
Syntax versus semantics
There are two different issues in defining a programming language:
1. Syntax. What does it mean for a statement to be syntactically correct? For example, you know that the following if-statement is not correct because it is missing an open parenthesis:
if x < 3) x= x+2;
You also now know that if x is of type int, the Java statement x= 5.3; is syntactically incorrect.
If a program is not syntactically correct, it cannot be executed (or run, or carried out). Whether you are using Matlab or Python or Java, your program will not be executed if it contains a syntax error.
A Java compiler is a program that (1) checks that a Java program is syntactically correct and, if it is, (2) translates the Java program into the Java virtual machine language so it can be executed.
2. Semantics. Semantics has to do with meaning. For a programming language, semantics deals with how a program is executed. This requires knowing how each statement of the language is executed. For example, what does it mean to execute, or carry out, the assignment x= x+2; ? We'll see that next.
0. Presenting an algorithm in English
Essentially, an algorithm is a sequence of steps to be executed. Some algorithms are written in English, some in Java, some in Python, some in a mixture of English and a programming language. In this 2.5-minute video, we explain how English steps in an algorithm should be written, using as our model the cooking recipe. Read it here: 01presentingAlgorithmsB.pdf
1. Executing the assignment statement
2. Evaluating the new-expression
We have explained and demoed the new-expression. You have used it in some progams. Yet, when asked how to evaluate it many student cannot answer reasonably. They hem and haw, They ask "What do you mean by evaluating it?" This 2-minute video once again give the algorithm for evaluating a new-expression and demoes its evaluation. We urge you to watch it carefully. Read it here: 02new-expression.pdf
Try practice 1 (see above) on the three steps in evaluating the new-expression. Then do two exercises on this page.
3. Executing (or evaluating) method calls
It is important to understand how a method call is executed. It will help you understand when local variables are creating during a call, and it will help you understand how recursion works. When debugging a program, it can help you find the source of an error. See this page to learn about executing method calls.
4. Executing a try-statement.
You already have had a tutorial and problem set on exception handling. A lot of your questions on exception handling can be answered by understanding exaclty how a try-statement is executed. The try-statment has the form
try <try-block>
catch (<throwable-class-1> e) <catch-block-1>
...
catch (<throwable-class-n> e) <catch-block-n>where a throwable-class is class Throwable or on of its subclasses.
Look at this pdf file for the algorithm for executing the try-statement.