Please enable JavaScript to use CodeHS

Chapter 1

Primitive Types

1.4 Expressions and Assignment Statements

Arithmetic Expressions

Arithmetic expressions allow your to perform mathematical operations within Java. Such expressions can be used for basic math and even more complex algorithms.

Arithmetic Operators

Operator Description
+ Addition operator (Ex. 1 + 1 = 2)
- Subtraction operator (Ex. 2 - 1 = 1)
* Multiplication operator (Ex. 2 * 2 = 4)
/ Division operator (Ex. 6 / 2 = 3)
% Modulus operator (Ex. 10 % 3 = 1)

Addition Operator

The addition operator allows you to add values together. Here is an example of the addition operator in Java:

public class AdditionOperator
{
  public static void main(String[] args)
  {
    int firstVal = 5;
    int secondVal = 2;

    int firstPlusSecond = firstVal + secondVal;

    // The output will be '7'
    System.out.println(firstPlusSecond);
  }
}
Plain text

Subtraction Operator

The subtraction operator allows you to subtract values from one another. Here is an example of the subtraction operator in Java:

public class SubtractionOperator
{
  public static void main(String[] args)
  {
    int firstVal = 4;
    int secondVal = 2;

    int firstMinusSecond = firstVal - secondVal;

    // The output will be '2'
    System.out.println(firstMinusSecond);
  }
}
Plain text

Multiplication Operator

The multiplication operator allows you to multiply values. Here is an example of the multiplication operator in Java:

public class MultiplicationOperator
{
  public static void main(String[] args)
  {
    int firstVal = 4;
    int secondVal = 2;

    int firstMultSecond = firstVal * secondVal;

    // The output will be '8'
    System.out.println(firstMultSecond);
  }
}
Plain text

Division Operator

The division operator allows you to divide values. Here is an example of the division operator in Java:

public class DivisionOperator
{
  public static void main(String[] args)
  {
    int firstVal = 6;
    int secondVal = 3;

    int firstDivSecond = firstVal / secondVal;

    // The output will be '2'
    System.out.println(firstDivSecond);
  }
}
Plain text

Types of Division

There are multiple types of division that can be performed in Java. These forms include: integer division, double division, and mixed division.

Integer Division
If you divide two integers you will be returned an integer value.

So in this case, while 2 / 5 equals 2.5, in Java 2 / 5 = 2. This always holds true, unless the type is specified as a double. Whenever you divide two integers, the return value is always truncated.

Double Division
Dividing doubles is different from dividing integers.

When you divide two doubles you will be returned a double. In this case,2.0 / 5.0 = 2.5 will be your result.

Mixed Division
Mixed division is when you divide a double by an integer or an integer by a double.

When you use mixed division in your program you will be returned a double. This is true, unless the type is specifically defined as an integer.

Consider the following piece of code:

double x = 2;
int y = 5;

double outXY = x / y;   // 0.4

double outYX = y / x;   // 2.5

int outInt = (int)(y / x);  // 2
Plain text

Arithmetic Exceptions

No matter which data type you use, dividing by zero will cause your programs to throw an ArithmeticException. Exceptions are unwanted or unexpected events which occur during the execution of a program. In this case, dividing by zero causes the variable to store the value of undefined, which cannot be used in arithmetic expression. The program throws the ArithmeticException to warn programmers that there is an issue that needs to be resolved.

Modulus Operator

The modulus operator allows you to divide two numbers and get the remainder. Here is an example of the modulus operator in Java:

public class ModulusOperator
{
  public static void main(String[] args)
  {
    int firstVal = 17;
    int secondVal = 5;

    int firstModSecond = firstVal % secondVal;

    // The output will be '2' since the remainder of 17 / 5 is 3.
    System.out.println(firstModSecond);
  }
}
Plain text

Operation Precedence

It is important to remember that the order of operations still applies. In instances where you have multiple operators with the same precedence the order will be left to right.

Operator Precedence
( ) Parentheses 1st
* / Multiplication and Division 2nd
+ - Addition and Subtraction 3rd

Calculator

Temperature Conversion

Tricky Java

Check Your Understanding

  1. Incorrect Correct No Answer was selected Invalid Answer

    What is the result of this expression?

    54 % 5
    Plain text

Exercise: Fractions

In this program you will initialize 4 integers that represent each part of two fractions, namely the numerator and denominator of the first fraction and the numerator and denominator of the second fraction.

Your program should subtract the two fractions and print out the result.

For example, a sample program run might look like this:

The numerator of the first fraction is 1
The denominator of the first fraction is 2
The numerator of the second fraction is 1
The denominator of the second fraction is 5
The difference of 1/2 - 1/5 = 3/10
Plain text

Note: you do not need to reduce these fractions. Be sure to test your code for positive and negative integers by changing the values of your initialized variables.

Remember, if you have two fractions you subtract them with:

     a         c        ad - bc
    ___   -   ___    =   ______
     b         d           bd
Plain text