Skip to content
Jay edited this page Sep 5, 2023 · 3 revisions

Overview

Operators are used to compare, alter or otherwise edit two different types
There are a few groups that you could put operators in:

  • Arithmetic: These operators alter the types used with them by adding, subtracting, etc
  • Comparison: These compare two types together
  • Logical: These compare 2 or more expressions and return true if the logic is true. And and Or operators
  • Assignment: These assign types
  • Ternary: Takes an if statement and shortens it to one line

There are also Bitwise Operators but these are used for binary operations. Which is not what we are using

Arithmetic Operators

These are pretty much your standard math operators that would be used when computing values

The following operators fall under this category:

Operator Name Function Example
+ Addition Adds one type to another int i = 5 + 7;
- Subtraction Subtracts one type from another int i = 5 - 2;
* Multiplication Multiplies two types together int i = 7 * 4;
/ Division Divides one type from another int i = 4 / 2;
% Modulus Returns the remainder after division int i = 5 % 2;
++ Increment Increments the value by 1 int i = 3++;
-- Decrement Decreases the value by 1 int i = 3--;

When using / on an int, any decimal that gets left over will be removed without rounding.
It's usually better to use a double or float when doing division in Java

For other operations such as exponents, additional imports are required
The example shown below is for exponents and uses the java.lang.Math import

import java.lang.Math;

int i = 7

i = Math.pow(8, i); // 8 to the power of i

System.out.println(i);

Comparison Operators

These operators compare two instances of a type together

These consist of greater than, less than, equals to and all the mix

Operator Name Function Example
> Greater Than Sees if an item is greater than another 7 > 4
< Less Than Sees if an item is less than another 7 < 4
== Equals To Sees if an item equals another 9 == 6
!= Does Not Equal To Sees if an item does not equal another 2 != 5
>= Greater Than or Equals To Sees if an item is greater or equal to another 4 >= 7
<= Less Than or Equals To Sees if an item is less or equal to another 4 >= 7

Logical Operators

These are your And and Or operators as well as the Not operator

Operator Name Function Example
&& And Sees if two expressions are true i > 5 && i < 9;
|| Or Sees if at least one expression is true i = 5 || i = 9;
! Not Reverses the output !(i = 5 || i = 9);

Ternary Operator

This operator takes an if statement and shortens it down into one line. Helpful for organization and shortening of code. But is a little confusing at first glace.

The Ternary Operator uses 3 expressions in the syntax: Expression1 ? Expression2 : Expression3

Say we had this if statement

package tests

class Ternary {
    public static void main(String[] args){
        int a;
        int b = 6;

        if (b == 6;){ // Expression1
            a = 1; // Expression2
        }
        else{
            a = 2; // Expression3
        }
        
        System.out.println(a); // Would print the value of a
    }
}

We can turn this into a ternary operator by taking Expression1 (b == 6), Expression2 (a = 1), and Expression3 (a = 2) and plugging them into the ternary operator syntax. Expression1 ? Expression2 : Expression3

If you plug in the expressions the new code should as followed

package tests

class Ternary {
    public static void main(String[] args){
        int a;
        int b = 6;

        b == 6 ? a = 1 : b = 2; // Replaced with a single ternary operator
        
        System.out.println(a); // Would print the value of a
    }
}

This code does the same thing except uses a ternary operator instead of a if-else statement.

Note: Ternary operators don't replace if-else statements. It can simplify simple if-else statements but can be more complicated to use a lot of ternary operators on more complicated if-else statements

Clone this wiki locally