Java Operators

In this lesson, we will look at the Java Operators. Operators can be defines as the most fundamental unit of any programming language. It can be used for performing multiple operations on different values and variables. It also provides multiple group of operators. These are symbols that holds the mathematical operations. There are different types of operators based upon their values.

We will be walking around different Java operators in this tutorial and learning about their functionalities and how to use them in depth.

1. Java Operators Type

Operators can be divided into multiple categories based upon their usage and functionalities. Following are some types of operators in Java that are defined below:

  1. Arithmetic Operator
  2. Assignment Operator
  3. Relational Operator
  4. Logical Operator
  5. Unary Operator
  6. Bit-wise Operator

2. Arithmetic Operators

This type of operators defines the arithmetic operation to perform simple mathematical operations. These types of operators only works with the primitive value types and their boxed types such as int and Integer, long and Long. Let’s check the different types operators present in Arithmetic Operators:

OperatorNameDescriptionExample
+AdditionThis is used for adding two values.2+3=5
SubtractionThis is used for subtracting one value from another.3-2=1
*MultiplicationThis is used for multiplication of two values.2*3=6
/DivisionThis divides one value by another.3/2 = 1.5
%ModulusThis returns the remainder after division3%2 = 1
++IncrementThis increases the value of a variable by 1.x++ = 4(where x is 3)
DecrementThis decreases the value of a variable by 1.x– = 2(where x is 3)
Arithmetic Operators

2.1 Java Arithmetic Operators Example

Let’s see how to implement the arithmetic operators in any java program.

public void arithmeticOperators() {
    // Addition
    int eight = 4 + 4;
    String title = "java" + "dev" + "journal";

    // Subtraction
    int three = 10 - 7;
    int negativeResponse = 7– 10;

    // Multiplication
    int hundred = 20 * 5;
    int fifteen = -5 * -3;

    // Division
    int two = 10 / 5;
    int seven = 15 / 2;

    // Modulo
    int one = 13 % 2;
    int zero = 12 % 5;
}

3. Assignment Operators

The Assignment Operators are used for assigning values into variables. There are mainly two types of assignment operators in java i.e.

  • Simple Assignment Operator.
  • Compound Assignment Operator

3.1. Simple Assignment operator

It is a straightforward operator that is used for assigning values into variables. Equal symbol (=) represents assignment operators in java. It assigns the value on its right to the operand on its left.

public void arithmeticOperators() {
    // Assigning values
    int assignFour = 4;
    String title = "Hello User";
}

3.2. Compound Assignments

The compound assignments are basically used for assigning the combined value of arithmetic operators with the single assignment operator. For Instance, writing “a = a + 2” in a compound way converts to: “a += 2”. Let’s check some supported compound assignments in Java through below examples.

public void assignmentOperators() {
    int a, b, c, d, e = 10
    a += 5; // a = 15, same as a = a + 5
    b -= 5; // b = 5, same as b = b - 5
    c *= 5; // c = 50, same as c = c * 5
    d /= 5; // d = 2, same as d = d / 5
    e %= 5; // e = 0, same as e = e % 5
}

4. Relational Operator in Java

These are comparison operators that are used for making the comparison among values. We can compare both the values and the variables using these operators.

4.1. EqualTo Operator

We can use the double equals symbol (==) for comparing two values and check if both the values equate to each other or not. This will return true if both the values or variables are equal to each other.

public void relationalOperators() {
    int number1 = 5; // Assigning values
    int number2 = 5;
    boolean theyAreEqual = number1 == number2; // returns true
}

4.2. The Not Equal To Operator

This operator is used for checking the opposite of the equal operator i.e. (!=). This returns TRUE when both the values are not equal.

4.3. Less Than Operator

This compares two value with the less than operator (<) that returns true when the value on left hand side is less than the value on right hand side.

public void relationalOperators() {
    int number1 = 7;
    int number2 = 5;
    boolean notEqual = number1 != number2; // 7 != 5
    boolean greater = number1 > number2; // 7 > 5
    boolean lesser = number2 < number1; // 5 < 7
}

4.4. Greater Than Operator

This compares two value with the greater than operator (>) that returns true when the values on left hand side is greater than the value on right hand side.

5. Logical Operators in Java

Java mainly has two logical operators, i.e. Logical AND and OR operators. Their function basically helps the user to achieve specific operations. A Logical operator is mostly used with two operands which are variables or define the expressions that can be evaluated as boolean. Let’s dive deep into these logical operators.

5.1. Logical AND Operator

The Logical AND (&&) operator returns true when both the operands are true. The Logical AND operator performs the specific operation:

Java Operators
public void logicalOperators() {
    int number1 = 7;
    int number2 = 5;
    boolean andTrue = (number1 > 0) && true; // true
    boolean andFalse = (number1 > 0) && false; // false
    boolean notEqual = (number1 > 0) && (number1 > number2); //true
}

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.