Operators in Java

Till now we have learned about the different Data Types, variables. Now we need to learn, how to use them.

It is time to learn how to use the variables and data types in real practice. The information stated below will be helpful for any beginner to learn the basics of operators and their usage with the help of examples.

Java Operators. What are they?

When writing a code in Java, we make use of operators in almost every step starting from methods to classes to logics, etc. Operators are the special symbols used in programming languages that help us perform different operations (like for addition we use ‘+’ operator, for subtraction we use ‘-‘operator, and so on), which will be discussed in detail in the below sections.

Types of Java Operators

Since there are a number of different kinds of operations that can be performed with these operators, hence they have been broadly divided into the following categories:

  1. Arithmetic Operators

These operators are used to perform addition, subtraction, multiplication and division between different operands.

Following are the operators:

OperatorDescription
+Addition (String Concatenation can also be performed)
Subtraction Operator
*Multiplication Operator
/Division Operator
%Remainder Operator

Program to illustrate the use of the Arithmetic operators

public class Test {

	public static void main(String[] args) {

		int var1 = 10, var2 = 3;
		double output;
		String string1 = "Hello", string2 = "QATechHub", stringOutput;

		output = var1 + var2;// Addition operator
		System.out.println("var1 + var2 = " + output);

		output = var1 - var2;// Subtraction operator
		System.out.println("var1 - var2 = " + output);

		output = var1 * var2; // Multiplication operator
		System.out.println("var1 * var2 = " + output);

		output = var1 / var2;// Division operator
		System.out.println("var1 / var2 = " + output);

		output = var1 % var2;// Remainder operator or Modulus Operator
		System.out.println("var1 % var2 = " + output);

		stringOutput = string1 + string2;// Addition operator for Concatenating two Strings
		System.out.println("s1 + s2 = " + stringOutput);
	}
}

Output –

var1 + var2 = 13.0
var1 - var2 = 7.0
var1 * var2 = 30.0
var1 / var2 = 3.0
var1 % var2 = 1.0
s1 + s2 = HelloQATechHub


The above example illustrates the usage of Arithmetic operators. Moreover, the “+” operator could be used to add two numeric values as well as for the concatenation of two strings.

  • Relational Operators

These operators are used to match two values (and check the relation between them) and give a Boolean value as an output, which is either true or false. It checks if a value is equal to or greater than or less than or not equal to.

The Relational Operators are:

OperatorDescription
==equal to
!=not equal to
greater than
less than
>=greater than or equal to
<=less than or equal to

These operators are usually used in the decision making conditions, which will be further discussed in the coming sections.

public class Test {

	public static void main(String[] args) {

		// Usage of the Relational Operators

		System.out.println(100 > 20); // true
		System.out.println(100 <= 20); // false
		System.out.println(100 >= 20); // true
		System.out.println(100 == 20); // false
		System.out.println(100 != 20); // true

	}
}


The above example demonstrates the usage of the Relational operators used to show the comparison between different values.

  • Logical Operator

The logical operators ‘||’ (conditional-OR) and ‘&&’ (conditional-AND) operate on Boolean expressions.

The logical operators are:

OperatorDescription
||It is called as conditional-OR: Ouput – true if either of the boolean expression is true
&&It is called as conditional-AND Output – true if all boolean expressions are true

Example

public class Test {

	public static void main(String[] args) {

		// Usage of the Logical Operators

		// Usage of Local OR Operator
		System.out.println(true || true); // Output -true
		System.out.println(true || false); // Output -true

		// Usage of Logical AND operator
		System.out.println(true && true); // Output -true
		System.out.println(true && false); // Output -false

	}
}

In the above example, the logical OR gives the output as “true” either, if only one of the conditions is true, but in the case of Logical AND, both the conditions need to be true for the output to be true.

** These operators are usually used in the Decision Making Loop statements.

  • Conditional Operator

Also known as Ternary Operators, are mostly used as the replacement for the if-else statement and is preferred while writing hefty programs, since it helps reduce the line of code.

Syntax-  Output=Statement? Value1  : Value2

It works in a very similar fashion the if-else statement, as demonstrated below:

  • If Statement is true , then Value1 is assigned to the Output
  • If Statement is false , then Value2 is assigned to the Output

Example

public class Test {

	public static void main(String[] args) {

		int val1 = 10, val2 = 20;
		String Output;

		Output = val1 > val2 ? "Val1 is Greater" : "Val2 is Greater";
		System.out.println("Which Value is greater :: " + Output);

	}
}


Output:

Which Value is greater :: Val2 is Greater

Since the Expression (val1 > val2) is false, that’s the reason “Val2 is Greater” is assigned to output.

  • Assignment Operator

It is used for assigning the values to the variables in Java. Assignment operator assigns the value (on right side) to the variable (on left side)

Example

int houseNumber;
houseNumber = 10;

In the above example, houseNumber variable is being assigned the value 10 using the = operator.

Assignment operators are:

OperatorDescription
=Assign the value to the variable on left side
+ =Add value on right side to the current value of the variable and assign the new value to the variable
– =Subtract value on right side from the current value of the variable and assign the new value to the variable
* =Multiply value on right side to the current value of the variable and assign the new value to the variable
/ =Divide the value on right side by the current value of the variable and assign the quotient value to the variable
% =Divide the value on right side by the current value of the variable and assign the remiander value to the variable

Example –

public class Test {

	public static void main(String args[]) {
		int var1 = 10;
		int var2 = 20;
		int var3 = 0;

		// Example to show the working of Different assignment operators

		var3 = var1 + var2;
		System.out.println("var3 = var1 + var2 = " + var3);

		var3 += var1;
		System.out.println("var3 += var1  = " + var3);

		var3 -= var1;
		System.out.println("var3 -= var1 = " + var3);

		var3 *= var1;
		System.out.println("var3 *= var1 = " + var3);

		var1 = 10;
		var3 = 15;
		var3 /= var1;
		System.out.println("var3 /= var1 = " + var3);

		var1 = 10;
		var3 = 15;
		var3 %= var1;
		System.out.println("var3 %= var1  = " + var3);
	}
}

Output-

var3 = var1 + var2 = 30
var3 += var1  = 40
var3 -= var1 = 30
var3 *= var1 = 300
var3 /= var1 = 1
var3 %= var1  = 5
  • Shift Operators

In Java, we have the operators that helps us in operating bitwise and bit shift operation on integral parts.

Shift Operators are:

OperatorDescription
<< Left Shift Operator
>> Right shift Operator
  1. Left Shift Operator

It shifts all the bits in a number to the left side of a specified number of times(Shifting the bit patterns)

Example-

public class Test {

	public static void main(String args[]) {

		// Left Shift Operator

		System.out.println(8 << 2);// 8*2^2=10*4=32
		System.out.println(100 << 3);// 100*2^3=10*8=800

	}
}


2. Right Shift Operator

It shifts all the bits in a number to the Right side of a specified number by the times (Shifting the bit patterns)

Example

public class Test {

	public static void main(String args[]) {

		// Right Shift Operator

		System.out.println(8 >> 2);// 8/2^2=8/4=2
		System.out.println(100 >> 3);// 100/2^3=100/8=12

	}
}

  • Increment/Decrement Operators

It is also called as the unary operator. It is used for incrementing or decrementing the value of the operands.

Increment/Decrement Operators are:

OperatorDescription
++Increments the value by 1
Decrements the value by 1

Note – Unary operators are usually used in the loops while programming.

Example-

		int a = 100;

		// Usage of the Unary Operators
		System.out.println(a++); // It first uses the 'a' variable value(i.e 100) and then increments it by
									// 1(101)

		System.out.println(++a); // It takes the last value of variable 'a'(101) and increments it by 1(102)

		System.out.println(a--); // It first uses the last value of variable 'a'(102) and then decrements it by
									// 1(101)

		System.out.println(--a); // It takes the last value of variable 'a'(101) and decrements it by 1(100)
	}
}


Output

100
102
102
100

In the above mentioned example, we have used the increment and the decrement operators at prefix (++a) and postfix (a++) position.

There is a difference in the way the Prefix-Postfix values are used in the program, if it is:

  • a++ :: then it means use the original value of “a” first, then increment it afterwards
  • ++a :: then it means that increment the value first, then use it
  • a– :: then it means use the original value of “a” first, then decrement it afterwards
  • –a :: then it means that decrement the value first, then use it

Be very cautious while placing these operators in either in the front or behind the variables, because they may change the output of the program.

Note: These operators may only be applied on the variable not on the constants

Example

  • A ++  -> Valid statement
  • 5++  -> Invalid Statement
  • Bitwise operator

These are used for interacting with individual bits of numbers. They can be applied to an integer, char, long shot, int m byte, etc. It is used for the manipulation at the Bit level.

Bitwise Operators are:

OperatorDescription
&Bitwise AND
^Bitwise exclusive OR
|Bitwise inclusive OR
Bitwise Operators

Example-

public class Test {

	 public static void main(String args[]) {
	      int a = 40;	/* 40 = 0010 1000 */
	      int b = 10;	/* 13 = 0000 1010 */
	      int c = 0;

	      c = a & b;        /* 12 = 0000 1100 */
	      System.out.println("a & b = " + c );

	      c = a | b;        /* 42 = 0010 1010 */
	      System.out.println("a | b = " + c );

	      c = a ^ b;        /* 34 = 0100 0101 */
	      System.out.println("a ^ b = " + c );

	    
	   }
}

Thanks, for reading out this article. Hope you have enjoyed reading it out. If you have any questions or queries. Please feel free to write to us at support@qatechhub.com

Tejasvi Nanda

About the Author

Tejasvi Nanda