Skip to main content

Section 2.1 Worked Example: Declaration and Initialization

Subgoals for evaluating an assignment statement.

  1. Determine resultant data type of expression
  2. Update variable for pre operators based on side effect
  3. Solve arithmetic equation, operator precedence
  4. Is the Left Hand Side (LHS) of the assignment statement a variable? Check the data type of the value on right hand side (RHS) against data type of LHS variable.
  5. Update variable for post operators based on side effect

Subsection 2.1.1

You can watch this video or read through the content below it.
Evaluate these statements. If invalid, give the reason. If valid, what are the values of the variables?
int alpha, beta, gamma;
double omega, theta, lambda;
alpha = 42;
beta = 1;
omega = 2.5;
theta = 4;
gamma = 0.0;

Subsection 2.1.2 SG1: Determine resultant data type of expression

  • The code block can be broken into 2 kinds of statements. The first two lines are the declaration statements, followed by additional assignments.
int alpha, beta, gamma;
double omega, theta, lambda;
  • In the first line, alpha, beta, and gamma are all declared as int type variables.
  • Similarly, in the second line, omega, theta, and lambda are declared as double type variables.
  • Integers are whole numbers and doubles contain a decimal point and may contain fractional values.
alpha = 42;
beta = 1;
omega = 2.5;
theta = 4;
gamma = 0.0;
  • alpha = 42; the value on the RHS of the assignment statement is 42 which is a literal integer (whole number) value.
  • beta = 1; the value on the RHS of the assignment statement is 1 which is a literal integer (whole number) value.
  • omega = 2.5; the value on the RHS of the assignment statement is 2.5 which is a literal double (fractional number) value.
  • theta = 4; the value on the RHS of the assignment statement is 4 which is a literal integer (whole number) value.
  • gamma = 0.0; the value on the RHS of the assignment statement is 0.0 which is a literal double (fractional number) value.
  • What about lambda? What is it’s value? Because there is no assignment to the variable, its content is considered “unknown” or “garbage,” and it must be assigned before you could use lambda in another expression. In the Java specification, certain values like array contents, class variables, and instance variables are given default values like 0 or 0.0, but in our example, lambda and the others are all local variables which must be given values before their use. Some compilers, for simplicity, will give local variables the same default initializations, but it would be unwise to rely on this behavior that does not match the Java specification.

Subsection 2.1.3 SG2: Update variable for pre-increment or pre-decrement operators based on side effect

NOT USED IN THIS EXAMPLE

Subsection 2.1.4 SG3: Evaluate arithmetic expression according to operator precedence

NOT USED IN THIS EXAMPLE

Subsection 2.1.5 SG4: Is the Left Hand Side (LHS) of the assignment statement a variable? Check the data type of the value on right hand side (RHS) against data type of LHS variable.

  • alpha = 42; alpha has been declared as an integer and the data type of the expression on the RHS is an integer, so the value 42 can be copied into the alpha variable (the statement is valid). alpha now has the value 42.
  • beta = 1; beta has been declared as an integer and the data type of the expression on the RHS is an integer, so the value 1 can be copied into the beta variable (the statement is valid). beta now has the value 1.
  • omega = 2.5; omega has been declared as a double and the data type of the expression on the RHS is a double, so the value 2.5 can be copied into the omega variable (the statement is valid). omega now has the value 2.5.
  • theta = 4; theta has been declared as a double and the data type of the expression on the RHS is an integer. We can convert an integer into a decimal by simply adding “.0” to the value. The numeric value of any integer is not changed when converted to a decimal. The process of the computer adding the .0 to an int to convert it into a double is known as promotion, and is done automatically. This statement is valid. theta now has the value 4.0.
  • gamma = 0.0; gamma has been declared as an int and the data type of the expression on the RHS is a double. A double cannot be assigned to an integer variable and there is no automatic process to convert it. This results in a compilation error (incompatible types). If you copy the original code block into the runnable ActiveCode box, you can see the exact compilation error.
  • What about lambda? What is its value? Because there is no assignment to the variable, its content is considered “unknown” or “garbage,” and it must be assigned before you could use lambda in another expression. In the Java specification, certain values like array contents, class variables, and instance variables are given default values like 0 or 0.0, but in our example, lambda and the others are all local variables which must be given values before their use. Some compilers, for simplicity, will give local variables the same default initializations, but it would be unwise to rely on this behavior that does not match the Java specification.

Subsection 2.1.6 SG5: Update variable for post-increment or post-decrement operators based on side effect

NOT USED IN THIS EXAMPLE
Answer.
Answer: alpha is 42, beta is 1, omega is 2.5, theta is 4.0, lambda is unknown; Since we cannot assign a double to the int variable gamma, we have a compilation error on the final line.

Practice Pages.

Look at this code:
int delta; //declaration
delta = 15; //initialization
int epsilon = 26; //both declaration and initialization
  • The first line declares an integer variable (delta). The second line then copies the literal value 15 (an integer) to the variable delta. The first assignment to a variable is known as initialization.
  • These two lines (declaration and initialization) can be combined into a single statement, as seen on the third line involving epsilon.
  • This is true for any data type, not just integers. All the normal rules of assignment apply to the initialization step.
You have attempted of activities on this page.