Skip to content

SACHSTech/ICS3U1_Lesson_1_4_ArithmeticExpressions

Repository files navigation

1.4 Arithmetic Expressions & Casting

Here are some math operators you can use in your equations:

Symbol Operation
* multiplication
/ division
+ addition
- subtraction
( ) brackets to force order of operations BEDMAS
% modulus - whole number division BUT instead of finding the answer, it finds the remainder.

Example 1

class ArithmeticExpressions extends ConsoleProgram {
  
  public void run() {
    
    // Day Age Calculator
    int intAge;
    int intDays;

    intAge = readInt("How old are you? ");
    intDays = intAge * 365;

    System.out.print("You have been alive for this many days: " + intDays);
  }
}

Increment / Decrement

It is very common in programming to want to add one or subtract one to a variable. There are a couple ways to achieve this:

int counter = 1;
counter = counter + 1;

Here, we are adding one to the counter variable. In the first line, counter is assigned the value 1. In the next line, it is reassigning the value of counter to 2.

Instead of writing counter = counter + 1. There's a shortcut: counter++;

Similarly, we can use the same idea to decrement by 1: counter--

Example 2

What would be stored in the daysUntilSummer variable?

int daysUntilSummer = 180;
daysUntilSummer--;

Other Shortcuts

It is also common to want to modify the current value by adding/subtracting/multiplying/dividing another value.

Operation Shortcut
x = x + y x += y
x = x - y x -= y
x = x * y x *= y
x = x / y x /= y

Order of Operations

BEDMAS as you would use in math class applies in code as well. This is the order of operations that applies:

  1. Parentheses ()
  2. Multiplication and Division * / %
  3. Addition and Subtraction + -

NOTE: If there are multiple instances of the same precedence, read left to right.

Key Notes to Remember with Division

  • int / int = int
  • int / double = double
  • double / int = double
  • double / double = double

Example 3

What do you think 5 / 2 is in Java? (if they are both integers)
Since int / int always results in an integer, it gets truncated (meaning the decimal gets removed)



Casting

Sometimes we want to change a value from one type to another.
This process is called casting.

We can add the type we want in parentheses to explicitly cast to that type.

Changing double to int (explicit casting)

When converting from a larger type to a smaller type (like doubleint), Java does not do it automatically because information could be lost.
You must write the cast yourself. This is called a narrowing conversion.

Example 4

int x = (int)5.3;   // x becomes 5

Notice this truncates the decimal, it does not round.

Example 5

double x = 10.3;
int y = (int)x;     // y becomes 10

Changing int to double (implicit casting)

When converting from a smaller type to a larger type (like intdouble), Java does it automatically.
This is called implicit casting or widening.

Example 6 (implicit)

int x = 5;
double y = x;   // y becomes 5.0

You can also write the cast explicitly, though it isn’t required:

Example 7 (explicit, but optional)

int x = 5;
double y = (double)x;   // y also becomes 5.0

Casting in Expressions

Casting is especially important in arithmetic expressions.
Consider this example:

Example 8 (integer division issue)

int dollars = 100;
int numOfPeople = 40;
double dollarsPerPerson = dollars / numOfPeople;

What do you think the result will be?

Answer 2.0

Even though dollarsPerPerson is a double, the division is done using integers, so the result is 2, stored as 2.0.

To fix this, we can cast one of the operands to a double:

Example 9 (correct calculation)

int dollars = 100;
int numOfPeople = 40;
double dollarsPerPerson = (double)dollars / numOfPeople;  // 2.5

Now the calculation is done in floating-point arithmetic, giving the correct result.

Summary Table

Conversion Automatic? Name Example
intdouble Yes Widening (implicit) double y = x;
intdouble Optional Explicit cast allowed double y = (double)x;
doubleint No Narrowing (explicit) int y = (int)z;

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages