Skip to content

Operators Used In Java Programming Language

In this article, you are going to learn different types of operators used in Java Programming Language.

Operators are used to performing certain operations(addition, subtraction, division, comparison etc.) so as to produce the desired output.

The values on which these operations are applied are called operands.

An operation can be performed on one or more operands in order to get a new value(or to modify the given value) depending on the type of result you desire.

Now let’s go through how these operators are categorised and how to use these operators in java programming language.

Also Read how to Modifiers Used In Java Programming Language

TYPES OF OPERATORS IN JAVA PROGRAMMING LANGUAGE

  1. Arithmetic Operators
  2. Bitwise Operators
  3. Assignment Operators
  4. Logical Operators
  5. Relational Operators
  6. Ternary Operators
  7. Unary Operators
  8. Type Comparison Operator

[1] ARITHMETIC OPERATORS

Arithmetic operators are used to perform mathematical operations such as addition, subtraction, multiplication, division etc. Following are some arithmetic operators with their symbols.

  • Addition operator (+)
  • Subtraction operator (-)
  • Multiplication operator (*)
  • Division operator (/)
  • Modulus or Remainder operator (%)

Example:

public class ArithmeticExample {
public static void main(String args[]) {
int A = 30 ;
int B = 20 ;
System.out.println(“A+B =” + (A+B) ) ;
System.out.println(“A-B =” + (A-B) ) ;
System.out.println(“A*B =” + (A*B) ) ;
System.out.println(“A/B =” + (A/B) ) ;
System.out.println(“A%B =” + (A%B) ) ;
}
}

Output:

A+B = 50
A-B = 10
A*B = 600
A/B = 1
A%B = 10

[2] BITWISE OPERATORS

Bitwise Operators are used to perform operations on binary codes of any given number. They are applicable to data types such as long, short, int, char, and byte.

Following is a list of bitwise operators used in java language.

  • Bitwise AND operator (&)
  • Bitwise OR operator (|)
  • Bitwise XOR operator (^)
  • Bitwise Compliment operator (~)
  • Left Shift operator (<<)
  • Right Shift operator (>>)
  • Right Shift Zero Fill operator (>>>)

Suppose we have two numbers A = 40 and B = 15. In binary representation, they will be written as,

A = 00101000 and B = 00001111

After Performing above bitwise operations, you will get the following results.

A & B = 00001000 = 8
A | B = 00101111= 47
A^B = 00100111 = 39
~A = 11010111 = -41
A << 2 = 10100000 = 160
B >> 2 = 00000011 = 3
A >>> 2 = 00001010 = 10

Example:

public class BitwiseExample {
public static void main(String args[]) {
int A = 0 // Initializing the local variable
int B = 40 ;
int C = 15 ;

A = B & C ;
System.out.println(“B & C =” + A ) ;

A = B | C ;
System.out.println(“B | C =” + A ) ;

A = B ^ C ;  
System.out.println(“B ^ C =” + A ) ;

A = ~ B ;
System.out.println(“~ B =” + A ) ;

A = B << 2 ;
System.out.println(“B << 2 =” + A ) ;

A = C >> 2 ;
System.out.println(“C >> 2 =” + A ) 

A = B >>> 2 ;
System.out.println(“B >>> 2 =” + A ) ;

}
}

Output:

B & C = 8
B | C = 47
B ^ C = 39
~ B = -41
B << 2 = 160
C >> 2 = 3
B >>> 2 = 10

[3] ASSIGNMENT OPERATORS

As the name suggests, an assignment operator is used to assign value of one operand(right side) to another operand(left side). Following is a list of assignment operators used in java programming language.

  • Assignment operator (=)
  • Assignment-addition operator (+=)
  • Assignment-subtraction operator (-+)
  • Assignment-multiplication operator (*=)
  • Assignment-division operator (/=)
  • Assignment-remainder operator (%=)
  • Assignment-left shift operator (<<=)
  • Assignment-right shift operator (>>=)
  • Assignment-bitwise operator (&=)
  • Assignment-bitwise exclusive OR operator (^=)
  • Assignment-bitwise inclusive OR operator (|=)

Example:

public class AssignmentExample {
public static void main(String args[]) {
int A = 0 // Initializing the local variable
int B = 20 ;
int C = 40;

A = B + C ;
System.out.println(“Sum B + C =” + A ) ;

A += B ;
System.out.println(“Sum A += B =” + A ) ;

C -= B ;  
System.out.println(“Difference C -= B =” + C ) ;

B *= C ;
System.out.println(“Multiplication B *= C =” + B ) ;

A /= B ;
System.out.println(“Division A /= B =” + C ) ;

C %= B ;
System.out.println(“Modulus C %= B =” + C ) 

C <<= 2 ;
System.out.println(“Left Shift C <<= 2 =” + C ) ;

C >>= 2 ;
System.out.println(“Right Shift C >>= 2 =” + C ) ;

C &= B ;
System.out.println(“Bitwise AND operation C &= B =” + C ) ;

C ^= B ;
System.out.println(“Bitwise exclusive operation C ^= B =” + C ) ;

C |= 1 ;
System.out.println(“Bitwise inclusive operation C |= 1 =” + C ) ;}
}

Output:

Sum B + C = 60
Sum A + B = 80
Difference C -= B = 20
Multiplication B *= C = 400
Division A /= B = 0
Modulus C %= B = 20
Left Shift C <<= 2 = 80
Right Shift C >>= 2 = 20
Bitwise AND operation C &= B = 16
Bitwise exclusive(XOR) operation C ^= B = 384
Bitwise inclusive(OR) operation C |= 1 = 385

[4] LOGICAL OPERATORS

Logical Operators are used to perform operations on variables of boolean data type. There are three logical operators used in java programming language which are listed below with their symbolic representation.

  • Logical AND (&&)
  • Logical OR ( || )
  • Logical NOT ( ! )

Suppose two variables ‘X’ and ‘Y’ holds boolean value ‘false’ and ‘true’ respectively. On performing above three logical operations on these variables, you will get the following results.

X && Y = false
X || Y = true
! (X || Y) = false

Example:

public class LogicalExample {
public static void main(String args[]) {
boolean X = false ;
boolean Y = true ;

System.out.println(“X && Y =” + (X && Y) ) ;
System.out.println(“X || Y =” + (X || Y) ) ;
System.out.println(“! (X || Y) =” + ! (X || Y) ) ;}
}

Output:

X && Y = false
X || Y = true
! (X || Y) = false

[5] RELATIONAL OPERATORS

Relational operators are used to compare values of two variables(operands) which results in a boolean value. In java programming language, six relational operators are used which are listed below.

  • Equal to (==)
  • Not Equal to (!=)
  • Greater than (>)
  • Greater than or equal to (>=)
  • Less than (<)
  • Less than or equal to ( <=)

Let’s assume two variables A and B with values ’20’ and ’30’. The following results will be generated after performing above relational operations.

A == B = false
A != B = true
A > B = false
A < B = true
A >= B = false
A <= B = true

Example:

public class RelationalExample {
public static void main(String args[]) {
int A = 20 ;
int B = 30 ;

System.out.println(“A == B =” + (A == B) ) ;
System.out.println(“A != B =” + (A != B) ) ;
System.out.println(“A > B =” + (A > B) ) ;
System.out.println(“A < B =” + (A < B) ) ;
System.out.println(“A >= B =” + (A >= B) ) ;
System.out.println(“A <= B =” + (A <= B) ) ;}
}

Output:

A == B = false
A != B = true
A > B = false
A < B = true
A >= B = false
A <= B = true

[6] UNARY OPERATORS

Unary operators are used to perform operations like making a number positive or negative, incrementation or decrementation, etc.

Following are some unary operators used in java programming language.

  • Unary positive (+)
  • Unary negative (-)
  • Increment (++)
  • Decrement (–)

Example:

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

int A = 0 ;
int B = 50 ;

A = +B ;System.out.println(“Unary positive operation =” + A) ;

A = -B ;
System.out.println(“Unary negative operation =” + A) ;

A = B++ ;
System.out.println(“Incrementing the value by postfixing the operator =” + A) ;

A = ++B ;
System.out.println(“Incrementing the value by prefixing the operator =” + A) ;

A = B– ;
System.out.println(“Decrementing the value by postfixing the operator =” + A) ;

A = –B ;
System.out.println(“Decrementing the value by prefixing the operator =” + A) ;

}
}

Output:

Unary positive operation = 50
Unary negative operation = -50
Incrementing the value by postfixing the operator = 50
Incrementing the value by prefixing the operator = 52
Decrementing the value by postfixing the operator = 52
Decrementing the value by prefixing the operator = 50

[7] TERNARY OPERATOR (? 🙂

Ternary operator is used to assign a value to a variable out of two values with the help of an expression. The syntax is shown below.

Syntax: variable = expression ? value1 : value2 ;

Here, the variable takes value1 if the expression is true. If the expression is false, the variable takes value2.

Example:

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

int X, Y ;
X = 20 ;

Y = (X==20) ? 40 : 50 ;
System.out.println(“Value of Y =” + Y) ;

Y = (X == 10) ? 40 : 50 ;
System.out.println(“Value of Y =” + Y) ;}
}

Output:

Value of Y = 40
Value of Y = 50

[8] TYPE COMPARISON(INSTANCEOF) OPERATOR

This operator is used to determine whether an object is an instance of class, subclass or an interface. It basically compares the instance variable with its data type and returns a boolean value (true or false).

Example 1:

class InstanceOfExample1 {
public static void main(String[] args) {
Superclass objA = new Superclass( ) ;
Superclass objB = new Subclass( ) ;

System.out.println(“objA instanceof Superclass :” + (objA instanceof Superclass)) ;
System.out.println(“objA instanceof Subclass :” + (objA instanceof Subclass)) ;
System.out.println(“objA instanceof MyInterface :” + (objA instanceof MyInterface)) ;

System.out.println(“objB instanceof Superclass :” + (objB instanceof Subclass)) ;
System.out.println(“objB instanceof Subclass :” + (objB instanceof Subclass)) ;
System.out.println(“objB instanceof MyInterface :” + (objB instanceof MyInterface)) ;
}
}

class Superclass { }
class Subclass extends Superclass implements MyInterface { }
interface MyInterface { }

Output 1:

objA instanceof Superclass : true
objA instanceof Subclass : false
objA instanceof MyInterface : false
objB instanceof Superclass : true
objB instanceof Subclass : true
objB instanceof MyInterface : true

Example 2:

class InstanceOfExample2 {
public static void main(String[] args) {
String instituteName = “W3Training School” ;

boolean output = instituteName  instanceof String ;
System.out.println(“instituteName  instanceof String :”+ output) ;
}
}

Output 2:

instituteName  instanceof String : true

ALso Check : Modifiers Used In Java Programming Language

We have provided you the best possible description on different types of operators used in java programming language. Hope you like this post. For more updates and related information, stay connected to our blogs on java programming language.

Facebook
Twitter
LinkedIn
Pinterest