Skip Top Navigation Bar

Introduction to Arithmetic Expressions

Assignments

The assignment operator (=) is used to initialize a variable and to update it to a new value.

The variable that is being assigned a value is on the left side of the assignment operator and the value that is being assigned is on the right.

int myAge;
myAge = 18;

Expressions

In analyzing data, we will need to write expressions to do computations. The following arithmetic operators are used in expressions:

  • + for addition
  • - for subtraction
  • * for multiplication
  • / for division
  • % for modulus division

When using these operators, the resulting type depends on the data type of the operands.

  • If both operands are of type int, the result is of type int.
  • If one or more of the operands are of type double, the result is of type double.

This means that division functions in two different ways.

  • If both operands are of type int, the result truncates (removes) any remainder. This is considered integer division. For example:
    • 7 / 2 results in 3, the decimal portion is not included.
    • 4 / 2 results in 2, there is no remainder in this case.
  • If one or both operands are of type double, the result includes the remainder. For example:
    • 7.0 / 2 results in 3.5. Similarly, 7 / 2.0 and 7.0 / 2.0 will also result in 3.5.
    • 4.0 / 2 results in 2.0. Similarly, 4 / 2.0 and 4.0 / 2.0 will also result in 2.0.

Let's try this in the Java Playground.

Modulus divsion give the remainder when two numbers are divided. For example:

  • 7 % 2 results in 1, since 2 divides into 7, 3 whole times with a remainder of 1.
  • 17 % 3 results in 2, since 3 divides into 17, 5 whole times with a remainder of 2.
  • 4 % 2 results is 0, since 2 divides into 4, 2 whole times with no remainder.
  • 2 % 3 results in 2, since 3 divides into 2, 0 whole times and 2 is the remainder.

Let's try this in the Java Playground.

Order of Operations for Expressions

The order of operations works similarly how they work in mathematics. The order of operation is as follows:

  • parenthesis (( ))
  • mutliplication, division, and modulus working from left to right (*, /, %)
  • addition and subtraction working from left to right (+ and -)

Consider the following examples:

Example 1:

3 + 5 * 7 % 4
  • The first operation is multiplication 5 * 7, which is 35. Leaving us with 3 + 35 % 4.
  • The second operation is modulus division 35 % 4, which is 3. Leaving us with 3 + 3.
  • The last operation is addition 3 + 3, which is 6.
  • So, 3 + 5 * 7 % 4 evaluates to 6.

Let's try this in the Java Playground.

Example 2:

3 + 5 * (7 % 4)
  • The parenthesis override the order of operations causing the first operation to be modulus division 7 % 4, which is 3. Leaving us with 3 + 5 * 3.
  • The next operation is multiplication 5 * 3, which is 15. Leavning us with 3 + 15.
  • The last operation is addition 3 + 15, which is 18.
  • So, 3 + 5 * (7 % 4) evaluates to 18.

Let's try this in the Java Playground.

Example 3:

(3 + 5) * 7 % 4
  • The parenthesis override the order of operations causing the first operation to be additiona, 3 + 5, which is 8. Leaving us with 8 * 7 % 4.
  • The next operation is multiplication 8 * 7, which is 56. Leaving us with 56 % 4.
  • The last operation is modulus 56 % 4. Since 56 is divisible by 4, there is no remainder and the result is 0.
  • So, (3 + 5) * 7 % 4 evaluates to 0.

Let's try this in the Java Playground.

Example 4:

(3 + 5) * (7 % 4)
  • The parenthesis override the order of operations. Working left to right, the first operation that is evaluated is 3 + 5, which is 8. Leaving us with 8 * (7 % 4).
  • The next operation in parenthesis is then evaluated 7 % 4, which is 3. Leaving us with 8 * 3.
  • The last operation is multiplication 8 * 3, which is 24.
  • So, (3 + 5) * (7 % 4) evaluates to 24.

Let's try this in the Java Playground.

Assignment Operator

The assignment operator (=) happens after the expression is evaluated.

double taxes;
double price = 3.99;
double taxRate = 0.08;
taxes = price * taxRate;

Variables can be mutable (changeable). They can be reassigned new different values. Let's consider another example where a current score for a player gets updated.

int score = 100; 
score = score + 25;
  • The value of score starts off with the value of 100.
  • The expression score + 25 is evaluated to 125.
  • This value is assigned to score and now score has the value 125.

As program code gets increasingly more complicated, drawing a diagram to trace the value of the variable can be really helpful.

Addition with String objects

The addition operator can be used to append, or join, two String objects together or append a String object with a primitive value. In order to append a String with a primitive, the compiler will automatically create a String literal out of the primitive value. This is a String Conversion.

It is important to remember that items are added together from left to right. This applies to expressions that involve String literals as well.

Let's try some examples in the Java Playground.

In all the cases above, we could have assigned the resulting String literal to the variable.

Let's try this in the Java Playground.

Where this is really powerful, is if the primitive values are being stored in a variable of their own.

Let's try this in the Java Playground.

Order of Operations with String Literals and Primitives

  • The sum of 3 and 5 is computed first, resulting in 8.
  • Then this result (8) plus "is the value " is evaluated, resulting in "8 is the value".
  • Finally, "8 is the value " plus 8 is evalued, resulting in "8 is the value 8".

Let's try this in the Java Playground.

  • The sum of "What is the value of " and 3 is evaluated first, resulting in "What is the value of 3".
  • The sum of "What is the value of 3" and " plus " is evaluted second, resulting in "What is the value of 3 plus ".
  • The sum of "What is the value of 3 plus " and 5 is evaluated third, resulting in "What is the value of 3 plus 5".
  • The sum of "What is the value of 3 plus 5" and ? is evaluated forth, resulting in "What is the value of 3 plus 5? ".
  • The sum of "What is the value of 3 plus 5? " and 3 is evaluated fifth, resulting in "What is the value of 3 plus 5? 3".
  • The sum of "What is the value of 3 plus 5? 3" and 5 is evaluated sixth, resulting in "What is the value of 3 plus 5? 35".

You can use parenthesis to override the order of operation so to correct this output to be "What is the value of 3 plus 5? 8".

Let's try this in the Java Playground.

  • The sum of 3 and 5 in parenthesis is evaluated first, resulting in 8.
  • The sum of "What is the value of " and 3 is evaluated second, resulting in "What is the value of 3".
  • The sum of "What is the value of 3" and " plus " is evaluted third, resulting in "What is the value of 3 plus ".
  • The sum of "What is the value of 3 plus " and 5 is evaluated forth, resulting in "What is the value of 3 plus 5".
  • The sum of "What is the value of 3 plus 5" and ? is evaluated fifth, resulting in "What is the value of 3 plus 5? ".
  • The sum of "What is the value of 3 plus 5? " and 8 is evaluated fifth, resulting in "What is the value of 3 plus 5? 8".

Constants

There are times when we don't want a value to change while the program is running. In these cases, a constant can be used. A constant is a value that does not change once it is assigned a value. We use the keyword final in front of the data type when we create a constant and assign it the constant value. For example:

final double pi = 3.14;

Resources

Next Learn Tutorial

Learn: Introduction to Type Conversions with Casting