Lesson 3 – Operators in Java Copy

In the previous lesson. In this section we will see Java operators in action. Operators in Java are basically used to perform variety of operations on Java data types. There are four major types of Java operators:

  • Arithmetic Operators
  • Comparison Operators
  • Assignment Operators
  • Logical Operators

Arithmetic Operators

These operators, as the name suggests, perform arithmetic operations on the data. Arithmetic operators have been summarized in the following table:

Operator Name Functionality Example
+ Addition Performs addition on two numbers a + b
Subtraction Performs subtraction on two numbers a – b
* Multiplication Performs multiplication on two numbers a * b
/ Division Divides one value from another a / b
% Modulus Returns the division remainder a % b
++ Increment Increases the value of a variable by 1 ++ a
Decrement Decreases the value of a variable by 1 — a

Let’s see a simple example that demonstrates the use of some of the arithmetic operators:

public class MainClass {

public static void main(String[] args) {
// TODO Auto-generated method stub

int a = 50;
int b = 10;

int result = a + b;
System.out.println(result);

result = a - b;
System.out.println(result);

result = a * b;
System.out.println(result);

result = a / b;
System.out.println(result);

}

}

The output of the above script is as follows:

60
40
500
5

Comparison Operators

The comparison operators are used to compare two or more than two data values and return a Boolean value based on the comparison. Comparison operators have been summarized in the following table:

Operator Name Example
== Equal to a == b
!= Not equal a != b
> Greater than a > b
< Less than a < b
>= Greater than or equal to a >= b
<= Less than or equal to a <= b

Let’s see some of the comparison operators in action:

public class MainClass {

public static void main(String[] args) {
// TODO Auto-generated method stub

int a = 50;
int b = 10;

boolean result = a > b;
System.out.println(result);

result = a < b;
System.out.println(result);

result = a >= b;
System.out.println(result);

result = a <= b;
System.out.println(result);

result = a != b;
System.out.println(result);


}

}

The results of the above script are as follows

true
false
true
false
true

Assignment Operators

Assignment operators are used to assign value of one variable or constant to another variable. Java supports following assignment operators: .tg {border-collapse:collapse;border-spacing:0;} .tg td{font-family:Arial, sans-serif;font-size:14px;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:black;} .tg th{font-family:Arial, sans-serif;font-size:14px;font-weight:normal;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:black;} .tg .tg-cly1{text-align:left;vertical-align:middle} .tg .tg-1wig{font-weight:bold;text-align:left;vertical-align:top} .tg .tg-yla0{font-weight:bold;text-align:left;vertical-align:middle} .tg .tg-0lax{text-align:left;vertical-align:top}

Operator Name Example Is Equal to
= Assigns a = 10 a = 10
+= Add and Assign a += 5 a = a + 5
-= Subtract and Assign a -= 5 a = a – 5
*= Multiply and Assign a *= 5 a = a * 5
/= Divide and Assign a /= 5 a = a / 5
%= Take modulus and assign a %= 5 a = a % 5

The following example demonstrates the usage of some of the comparison operators:

public class MainClass {

public static void main(String[] args) {
// TODO Auto-generated method stub

int a = 50;

int result = a +=5;
System.out.println(result);

result = a -= 5;
System.out.println(result);

result = a *=5;
System.out.println(result);

result = a /=5;
System.out.println(result);

result = a %=5;
System.out.println(result);

}

}

The output of the above script is as follows:

55
50
250
50
0

Logical Operators

The logical operators are used to perform the logical AND , OR, NOT operations on the data and return a Boolean value. The logical operators supported by Java are as follows: .tg {border-collapse:collapse;border-spacing:0;} .tg td{font-family:Arial, sans-serif;font-size:14px;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:black;} .tg th{font-family:Arial, sans-serif;font-size:14px;font-weight:normal;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:black;} .tg .tg-cly1{text-align:left;vertical-align:middle} .tg .tg-yla0{font-weight:bold;text-align:left;vertical-align:middle}

Operator Name Description Example
&& AND operator Returns true if both operands are true a < 5 && a < 20
|| OR operator Returns true if one of the operands is true a < 5 ||  a < 20
! NOT operator Reverse the result !(x < 10)

Let’s take logical operators in action:

public class MainClass {

public static void main(String[] args) {
// TODO Auto-generated method stub

int a = 50;


boolean result = a > 10 && a < 100;
System.out.println(result);

result = a > 10 && a < 5;
System.out.println(result);

result = !(a > 10 && a < 5);
System.out.println(result);



}

}

The result of the above script is as follows:

true
false
true

What’s Next?

In this lesson, we covered operators in Java. In the next lesson we will study about arrays in Java.