Part 1

# Calculating with numbers

The basic mathematical operations are both familiar and straightforward: addition `+`, subtraction `-`, multiplication `*`, and division `/`. The precedence is also familiar: operations are performed from left to right with the parentheses taken into account. Expressions involving `*` and `/` are calculated before those involving `+` and `-`, as is customary in elementary school mathematics.

``````int first = 2;
System.out.println(first); // prints 2
int second = 4;
System.out.println(second); // prints 4

int sum = first + second; // The sum of the values of the variables first and second is assigned to the variable sum
System.out.println(sum); // prints 6``````

## Precedence and Parentheses

You can affect the order of operations by using parentheses. Operations within parentheses are performed before those outside them.

``````int calculationWithParentheses = (1 + 1) + 3 * (2 + 5);
System.out.println(calculationWithParentheses); // prints 23

int calculationWithoutParentheses = 1 + 1 + 3 * 2 + 5;
System.out.println(calculationWithoutParentheses); // prints 13``````

The example above can also be divided into steps.

``````int calculationWithParentheses = (1 + 1);
System.out.println(calculationWithParentheses); // prints 2
calculationWithParentheses = calculationWithParentheses + 3 * (2 + 5);
System.out.println(calculationWithParentheses); // prints 23

int calculationWithoutParentheses = 1 + 1;
calculationWithoutParentheses = calculationWithoutParentheses + 3 * 2;
calculationWithoutParentheses = calculationWithoutParentheses + 5;
System.out.println(calculationWithoutParentheses); // prints 13``````
:

An expression is evaluated where it occurs in the program's source code. As such, the evaluation can occur within a print statement, if the expression is used in calculating the value of the print statement's parameter.

``````int first = 2;
int second = 4;

System.out.println(first + second); // prints 6
System.out.println(2 + second - first - second); // prints 0``````

An expression does not change the value stored in a variable unless the expression's result is assigned to a variable or used as a parameter, for instance, in printing.

``````int first = 2;
int second = 4;

// the expression below does not even work, since
// the result is not assigned to any variable
// or given as a parameter to a print statement
first + second;``````
:

## Calculating and Printing

The command `System.out.println` prints the value of a variable. The string literal to be printed, which is marked by quotation marks, can be appended with other content by using the operation `+`.

``````int length = 42;
System.out.println("Length " + length);``````
Sample output

Length 42

``````System.out.println("here is an integer --> " + 2);
System.out.println(2 + " <-- here is an integer");``````
Sample output

here is an integer —> 2 2 <— here is an integer

If one of the operands of the operation `+` is a string, the other operand will be changed into a string too during program execution. In the example below, the integer `2` is turned into the string "2", and a string has been appended to it.

The precedence introduced earlier also apply here:

``````System.out.println("Four: " + (2 + 2));
System.out.println("But! Twenty-two: " + 2 + 2);``````
Sample output

Four: 4 But! Twenty-two: 22

Applying this knowledge, we can create an expression consisting of some text and a variable, which is evaluated in connection with the printing:

``````int x = 10;

System.out.println("The value of the variable x is: " + x);

int y = 5;
int z = 6;

System.out.println("y is " + y + " and z is " + z);``````
Sample output

The value of the variable x is: 10 y is 5 and z is 6

Once you have completed the previous exercise, try finding out the greatest possible multiplication that you can calculate. The reason behind the phenomenon you'll observe is that the value of an integer value is capped at the maximum of 231-1 (i.e. 2147483647). This is because integer variables are represented with 32 bits in the computer's memory. Variable representation is covered in more detail on the Computer Organization course.

## Division

Division of integers is a slightly trickier operation. The types of the variables that are part of the division determine the type of result achieved by executing the command. If all of the variables in the division expression are integers, the resulting value is an integer as well.

``````int result = 3 / 2;
System.out.println(result);``````
Sample output

1

The previous example prints 1: both 3 and 2 are integers, and the division of two integers always produces an integer.

``````int first = 3;
int second = 2;
double result = first / second;
System.out.println(result);``````
Sample output

1

The output 1 again, since first and second are (still) integers.

If the dividend or divisor (or both!) of the division is a floating point number, the result is a floating point number as well.

``````double whenDividendIsFloat = 3.0 / 2;
System.out.println(whenDividendIsFloat); // prints 1.5

double whenDivisorIsFloat = 3 / 2.0;
System.out.println(whenDivisorIsFloat); // prints 1.5``````
Sample output

1.5 1.5

An integer can be converted into a floating point number by placing a type-casting operation `(double)` before it:

``````int first = 3;
int second = 2;

double result1 = (double) first / second;
System.out.println(result1); // prints 1.5

double result2 = first / (double) second;
System.out.println(result2); // prints 1.5

double result3 = (double) (first / second);
System.out.println(result3); // prints 1.0``````
Sample output

1.5 1.5 1.0

The last example produces an incorrectly rounded result, because the integer division is executed before the type casting.

If the result of a division is assigned to an integer-type variable, the result is automatically an integer.

``````int integer = 3.0 / 2;
System.out.println(integer);``````
Sample output

1

The next example prints "1.5"; the dividend is converted into a floating-point number by multiplying it with a floating-point number prior to executing the division.

``````int dividend = 3;
int divisor = 2;

double result = 1.0 * dividend / divisor;
System.out.println(result);``````
Sample output

1.5

:

When a computer executes program code, it does it one command at a time, always advancing exactly as specified by the code. When a value is assigned to a variable, the same chain of events always occurs: the value on the right-hand side of the equality sign is copied and assigned to the variable on the left-hand side (i.e., copied to the location specified by that variable).

It's crucial for a programmer to understand that assigning a value to a variable always does the same thing.

Here's three common misunderstandings related to assigning a value to a variable:

• Viewing value assignment as a transfer instead of a copy operation: once `first = second` has been executed, it's often assumed that the value of the variable `second` has been moved to the value of the variable `first`, and that the variable `second` no longer holds a value, or that its value is 0, for instance. This is incorrect, as executing `first = second` means that the value in the position specified by `second` is merely copied to the place specified by the variable `first`. Hence, the variable `second` is not modified.
• Viewing value assignment as creating a dependency instead of being a copy operation: once `first = second` has been executed, it's often assumed that any change in the value of the variable `second` is automatically also reflected in the value of the variable `first`. This is incorrect; assignment — i.e., copying — is a one-off event. It only occurs when the program code `first = second` is executed.
• The third misunderstanding concerns the direction of copying: it's often thought that in executing `first = second` the value of the variable `second` is set as the value of the variable `first`. The confusion also manifests itself in situations where the programmer accidentally writes e.g. `42 = value` — fortunately, IDEs provide support on this issue too.

Perhaps the best way to understand a program's execution flow is through the use of pen and paper. As you're reading the program, write down the names of any new variables, while making a note of how the values of the variables in the code change line by line. Let's demonstrate the execution with the following program code:

``````line 1: int first = (1 + 1);
line 2: int second = first + 3 * (2 + 5);
line 3:
line 4: first = 5;
line 5:
line 6: int third = first + second;
line 7: System.out.println(first);
line 8: System.out.println(second);
line 9: System.out.println(third);``````

The execution flow of the program above has been broken down below.

Sample output

line 1: initiate a variable first line 1: copy the result of the calculation 1 + 1 as the value of the variable first line 1: the value of the variable first is 2 line 2: create the variable second line 2: calculate 2 + 5, 2 + 5 -> 7 line 2: calculate 3 * 7, 3 * 7 -> 21 line 2: calculate first + 21 line 2: copy the value of the variable first into the calculation, its value is 2 line 2: calculate 2 + 21, 2 + 21 -> 23 line 2: copy 23 to the value of the variable second line 2: the value of the variable second is 23 line 3: (empty, do nothing) line 4: copy 5 to the value of the variable first line 4: the value of the variable first is 5 line 5: (empty, do nothing) line 6: create variable third line 6: calculate first + second line 6: copy the value of the variable first into the calculation, its value is 5 line 6: calculate 5 + second line 6: copy the value of the variable second into the calculation, its value is 23 line 6: calculate 5 + 23 -> 28 line 6: copy 28 to the value of the variable third line 6: the value of the variable third is 28 line 7: print the variable first line 7: copy the value of the variable first for the print operation, its value is 5 line 7: print the value 5 line 8: print the variable second line 8: copy the value of the variable second for the print operation, its value is 23 line 8: print the value 23 line 9: print the variable third line 9: copy the value of the variable third for the print operation, its value is 28 line 9: we print the value 28

You'll find a step-by-step visualization of the previous program below, which goes through the program line by line — on each step, reflect on how the program ends up with the result that it prints.