Skip to content

Operators

In Java we distinguish between the following types of operators: * Assignment Operators * Arithmetic Operators * Conditional Operators * Equality and Relational Operators

Assignment Operators

With assignment operators we can assign (or give) a new value to a given variable. We distinguish:

  1. The '=' operator gives a value to a given variable:
int intValue = 5; // from this point on the intValue variable has the value of 5

If following the initial assignment we wish to change it's value we can do so at any moment:

int someValue = 6; // we declare someValue at 6
someValue = 7;     // we change it's value to 7
someValue = 4;     // now someValue has the value of 4


  1. The '+=' operator - this operator adds a value present at the right hand side to the variable and sums it up in the background automatically:

int a = 50;
a += 50;    // now the variable 'a' will have the value of 100

  1. The '-=' operator is similar in use to '+='. It subtracts the variable by the value provided at the right hand side:

int a = 50;
a -= 40;    // after subtracting the variable a will have the value of 10

  1. The '*=' operator works the same way as the other ones only the multiplication operator is used:

int a = 10;
a *= 10;    // after the operation the variable will have the value of 100

  1. For the '/=' operator the logic is the same, only the operation in place is the division:
int a = 200;
a /= 100;    // after the division operator, the result will be 2

Arithmetic Operators

The arithmetic operators similar to their mathematical counterparts perform basic operations on provided numbers or variables.

  1. The + - * / % - represent the basic operation of:

+* Additive operator ) - Subtraction operator Multiplication operator / Division operator %** Remainder operator

public class ArithmeticOperations {
    public static void main(String[] args) {
        int a = 5;
        int b = 10;
        int c = a + b;  // result: 15

        c = a - b;      // result: -5
        c = a * b;      // result: 50
        c = b / a;      // result: 2
        c = b % a;      // result: 0
    }
}

  1. Incrementation operator - adds 1 (one) to a given number. It can be used in two ways: post and pre. It's the equivalent of 'i=i+1'
  • Post Incrementation returns the value of a variable and then it modifies it:

int someVariable = 5;
System.out.print(someVariable++);   // At first the value of 5 will be printed, then it's value will be changed to 6.

  • Pre Incrementation at first the variable is modified then it returns it's value.

int someVariable = 5;
System.out.print(++someVariable);   // the value of 6 will be provided.

  1. Decrementation - decreases the value by 1 (one) and it is used in two forms: post and pre. It is the equivalent of 'i = i-1':
int someVariable = 10;
System.out.print(someVariable--);   // the value of 10 will be printed out, after execution the variable will have the value of 9.

someVariable = 15;
System.out.print(--someVariable);   // the value of 14 will be printed out.

Equality and Relational Operators

The equality and relational operators compare expressions and return it's logical values based on whether it's value is 'true' or not. The arguments for comparison can be numbers,strings,logical or object based. We most often use them within [conditional statements] (conditional statements.md). We can distinguish such operators:

  • Equality '==' which checks whether two arguments are equal:

int a = 5;
int b = 6;
System.out.print(a == b);   // the result will be 'false' as 5 is not equal to 6.

  • The '!=' operator check whether two arguments are not equal:

int a = 5;
int b = 6;
System.out.print(a != b);   //  the result will be 'true' as 5 is not equal to 6.

  • The greater than '>' or greater or equal than '>=' checks whether the first argument is greater or greater or equal than the other one:

int a = 6;
int b = 6;
System.out.print(a > b);    // the result will be false
System.out.print(a >= b);   // the result will be  true

  • The less than '<' operator or less or equal operator '<=' works this way:
int a = 5;
int b = 6;
System.out.print(a < b);     // this will return true
System.out.print(a <= b);    // this will also return true

Conditional Operators

They operate with arguments representing logical values which in turn also provide a logical result. We distinguish:

  • && - Conditional AND this takes two boolean arguments and also return a boolean result. The arguments is true only when both logical arguments are true (logical multiplication). Here is an example:

boolean boolValue1 = true;
boolean boolValue2 = false;
System.out.print(boolValue1 && boolValue2);     // The result of false will be the result.

  • || - Conditional OR, also known as the logical sum. It's true when at least one of it's arguments is true. Here is an example:

boolean boolValue1 = true;
boolean boolValue2 = false;
System.out.print(boolValue1 || boolValue2);     // the result true will be provided

  • ! - Logical NOT, also known as the negation. It can be interpreted as "not true that". An example is provided below:

boolean boolValue1 = true;
boolean boolValue2 = false;
System.out.print(!boolValue1);                   // the result false will be printed out
System.out.print(!(boolValue1 || boolValue2));     // the result false will also be printed out