You are on page 1of 30

Java Operators

Java Operators
Operators are symbols that perform operations on variables and values. For
example, + is an operator used for addition, while * is also an operator used for
multiplication.
Operators in Java can be classified into 5 types:

1. Arithmetic Operators
2. Assignment Operators
3. Relational Operators
4. Logical Operators
5. Unary Operators
6. Bitwise Operators

Sample Footer Text 2/8/20XX 2


1. Java Arithmetic Operators
Arithmetic operators are used to perform arithmetic operations
on variables and data. For example,
a + b;
Here, the + operator is used to add two variables a and b.
Similarly, there are various other arithmetic operators in Java.

Operator Operation

+ Addition

- Subtraction

* Multiplication

/ Division

Modulo Operation (Remainder after


%
division)
2/8/20XX 3
class Main {
public static void main(String[] args) {

// declare variables
int a = 12, b = 5;

// addition operator
System.out.println("a + b = " + (a + b));

// subtraction operator
System.out.println("a - b = " + (a - b));

// multiplication operator
System.out.println("a * b = " + (a * b));

// division operator
System.out.println("a / b = " + (a / b));

// modulo operator
System.out.println("a % b = " + (a % b));
}
}
2/8/20XX 4
Output
a + b = 17
a - b = 7
a * b = 60
a / b = 2
a % b = 2

In the above example, we have used +, -, and * operators to compute


addition, subtraction, and multiplication operations.

2/8/20XX 5
/ Division Operator
Note the operation, a / b in our program. The / operator is the division
operator.
If we use the division operator with two integers, then the resulting quotient
will also be an integer. And, if one of the operands is a floating-point
number, we will get the result will also be in floating-point.
In Java,

(9 / 2) is 4
(9.0 / 2) is 4.5
(9 / 2.0) is 4.5
(9.0 / 2.0) is 4.5

% Modulo Operator
The modulo operator % computes the remainder. When a = 7 is divided by b = 4, the
remainder is 3.
Note: The % operator is mainly used with integers.

2/8/20XX 6
2. Java Assignment Operators
Assignment operators are used in Java to assign values to variables. For example,
int age;
age = 5;
Here, = is the assignment operator. It assigns the value on its right to the variable on
its left. That is, 5 is assigned to the variable age.
Let's see some more assignment operators available in Java.

Operator Example Equivalent to

= a = b; a = b;

+= a += b; a = a + b;

-= a -= b; a = a - b;

*= a *= b; a = a * b;

/= a /= b; a = a / b;

%= a %= b; a = a % b; 2/8/20XX 7
Example 2: Assignment Operators

class Main {
public static void main(String[] args) {

// create variables
int a = 4;
int var;

// assign value using =


var = a;
System.out.println("var using =: " + var);

// assign value using =+


var += a;
System.out.println("var using +=: " + var);
}
}
2/8/20XX 8
Output
var using =: 4
var using +=: 8
var using *=: 32

2/8/20XX 9
3. Java Relational Operators
Relational operators are used to check the relationship between two operands.
For example,

// check if a is less than b


a < b;

Here, < operator is the relational operator. It checks if a is less


than b or not.
It returns either true or false.

2/8/20XX 10
Operator Description Example

== Is Equal To 3 == 5 returns false

!= Not Equal To 3 != 5 returns true

> Greater Than 3 > 5 returns false

< Less Than 3 < 5 returns true

>= Greater Than or Equal To 3 >= 5 returns false

<= Less Than or Equal To 3 <= 5 returns true

2/8/20XX 11
Example 3: Relational Operators
class Main {

public static void main(String[] args) {

// create variables

int a = 7, b = 11;

// value of a and b

System.out.println("a is " + a + " and b is " + b);

// == operator

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


Note: Relational operators are
// != operator
used in decision making and
System.out.println(a != b); // true
loops
// > operator

System.out.println(a > b); // false

// < operator

System.out.println(a < b); // true

// >= operator

System.out.println(a >= b); // false

// <= operator

System.out.println(a <= b); // true

} 2/8/20XX 12
4. Java Logical Operators
Logical operators are used to check whether an expression is true or false. They are used in decision making.

Operator Example Meaning

true only if both expression1 and


&& (Logical AND) expression1 && expression2
expression2 are true

true if either expression1 or


|| (Logical OR) expression1 || expression2
expression2 is true

true if expression is false and vice


! (Logical NOT) !expression
versa

2/8/20XX 13
Example 4: Logical Operators

class Main {
public static void main(String[] args) {

// && operator
System.out.println((5 > 3) && (8 > 5)); // true
System.out.println((5 > 3) && (8 < 5)); // false

// || operator
System.out.println((5 < 3) || (8 > 5)); // true
System.out.println((5 > 3) || (8 < 5)); // true
System.out.println((5 < 3) || (8 < 5)); // false

// ! operator
System.out.println(!(5 == 3)); // true
System.out.println(!(5 > 3)); // false
}
}
2/8/20XX 14
Unary Operators in Java
Java unary operators are the types that need only one operand to perform any operation like increment,
decrement, negation, etc. It consists of various arithmetic, logical and other operators that operate on a
single operand. Let’s look at the various unary operators in detail and see how they operate.
Operator 1: Unary minus(-)
This operator can be used to convert a positive value to a negative one.
Syntax:
llustration:
a = -10

2/8/20XX 15
// Java Program to Illustrate Unary - Operator

// Importing required classes


import java.io.*;
// Main class
class GFG {

// Main driver method


public static void main(String[] args)
{
// Declaring a custom variable
int n1 = 20;

// Printing the above variable


System.out.println("Number = " + n1);

// Performing unary operation


n1 = -n1;

Sample Footer Text 2/8/20XX 16


n1 = -n1;
// Printing the above result number
// after unary operation
System.out.println("Result = " + n1);
}
}
Output
Number = 20
Result = -20

2/8/20XX 17
Operator 2: ‘NOT’ Operator(!)
This is used to convert true to false or vice versa. Basically, it reverses the logical state of an operand.
Syntax:
!(operand)
Illustration:
cond = !true;
// cond < false

Example:
Java

/ Java Program to Illustrate Unary NOT Operator

// Importing required classes


import java.io.*;

Sample Footer Text 2/8/20XX 18


 // Main class
 class GFG {

 // Main driver method


 public static void main(String[] args)
 {
 // Initializing variables
 boolean cond = true;
 int a = 10, b = 1;

 // Displaying values stored in above variables


 System.out.println("Cond is: " + cond);
 System.out.println("Var1 = " + a);
 System.out.println("Var2 = " + b);

 // Displaying values stored in above variables


 // after applying unary NOT operator
 System.out.println("Now cond is: " + !cond);
 System.out.println("!(a < b) = " + !(a < b));
 System.out.println("!(a > b) = " + !(a > b));
 }
 } 2/8/20XX 19
Output:
Cond is: true
Var1 = 10
Var2 = 1
Now cond is: false
!(a < b) = true
!(a > b) = false

Sample Footer Text 2/8/20XX 20


Operator 3: Increment(++)
It is used to increment the value of an integer. It can be used in two separate ways:
3.1: Post-increment operator
When placed after the variable name, the value of the operand is incremented but
the previous value is retained temporarily until the execution of this statement and it
gets updated before the execution of the next statement.
Syntax:
num++
Illustration:
num = 5
num++ = 6

2/8/20XX 21
3.2: Pre-increment operator
When placed before the variable name, the operand’s value is incremented instantly.
Syntax:
++num
Illustration:
num = 5
++num = 6
Operator 4: Decrement(–)
It is used to decrement the value of an integer. It can be used in two separate ways:
4.1: Post-decrement operator
When placed after the variable name, the value of the operand is decremented but the previous values is retained
temporarily until the execution of this statement and it gets updated before the execution of the next statement.
Syntax:
num--
2/8/20XX 22
Illustration:
num = 5
num-- = 5 // Value will be decremented before execution of next statement.
4.2: Pre-decrement operator
When placed before the variable name, the operand’s value is decremented instantly.
Syntax:
--num
Illustration:
num = 5
--num = 5 //output is 5, value is decremented before execution of
++num = 6

2/8/20XX 23
Operator 4: Decrement(–)
It is used to decrement the value of an integer. It can be used in two separate ways:
4.1: Post-decrement operator
When placed after the variable name, the value of the operand is decremented but the previous values is retained
temporarily until the execution of this statement and it gets updated before the execution of the next statement.
Syntax:
num--
Illustration:
num = 5
num-- = 5 // Value will be decremented before execution of next statement.
4.2: Pre-decrement operator
When placed before the variable name, the operand’s value is decremented instantly

2/8/20XX 24
Operator 5: Bitwise Complement(~)
This unary operator returns the one’s complement representation of the input value or operand, i.e, with all bits inverted,
which means it makes every 0 to 1, and every 1 to 0.
Syntax:
~(operand)
Illustration:
a = 5 [0101 in Binary]
result = ~5

This performs a bitwise complement of 5


~0101 = 1010 = 10 (in decimal)

Then the compiler will give 2’s complement


of that number.
2’s complement of 10 will be -6.
result = -6

2/8/20XX 25
// Java program to Illustrate Unary
// Bitwise Complement Operator

// Importing required classes


import java.io.*;
// Main class
class GFG {

// Main driver method


public static void main(String[] args)
{
// Declaring a variable
int n1 = 6, n2 = -2;

// Printing numbers on console


System.out.println("First Number = " + n1);
System.out.println("Second Number = " + n2);

2/8/20XX 26
// Printing numbers on console after
// performing bitwise complement
System.out.println(
n1 + "'s bitwise complement = " + ~n1);
System.out.println(
n2 + "'s bitwise complement = " + ~n2);
}
}

Output:
First Number = 6
Second Number = -2
6's bitwise complement = -7
-2's bitwise complement = 1

2/8/20XX 27
Example program in Java that implements all basic unary operators for user
input:
import
Java java.util.Scanner;

public class UnaryOperators {


public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);

// fro user inputs here is the code.

// System.out.print("Enter a number: ");


// int num = sc.nextInt();
int num = 10;

int result = +num;


System.out.println(
"The value of result after unary plus is: "
+ result);

2/8/20XX 28
result = -num;
System.out.println(
"The value of result after unary minus is: "
+ result);
result = ++num;
System.out.println(
"The value of result after pre-increment is: "
+ result);
result = num++;
System.out.println(
"The value of result after post-increment is: "
+ result);
result = --num;
System.out.println(
"The value of result after pre-decrement is: "
+ result);
result = num--;
System.out.println(
"The value of result after post-decrement is: "
+ result);
}
}
2/8/20XX 29
Output
The value of result after unary plus is: 10
The value of result after unary minus is: -10
The value of result after pre-increment is: 11
The value of result after post-increment is: 11
The value of result after pre-decrement is: 11
The value of result after post-decrement is: 11

2/8/20XX 30

You might also like