Operator In Java

Operator In Java: Java is an object-oriented programming language that provides a variety of operators to manipulate and perform operations on values and variables. Operators in Java are symbols or keywords used to perform various operations such as arithmetic, logical, relational, bitwise, and assignment.

These operators play a crucial role in developing complex applications in Java by providing a way to interact with data and manipulate it to produce the desired results. Understanding the various operators in Java and how they work is essential for any Java programmer to develop efficient and effective programs. In this response, we will explore the different types of operators in Java and their functionality.

What is the Operator or Operator In Java?

An operator is a symbol that performs some operation. An operator acts on some variables called operands to get the desired result. According to the implementation, there are different types of operators are there like:

  • Unary Operator: If an operator acts on a single variable, then that is called a unary operator.
  • Binary Operator: If an operator acts on two variables, then that is called a binary operator.
  • Ternary Operator: If an operator acts on 3 variables, then that is called a ternary operator.

Also, in Java programming language, there are different operators according to classification. Below, we have mentioned the different types of operators based on their classification:

Unary Operator In Java

As the name indicates, unary operators act on only one operand. These operators are mainly used for the increment, decrement, or negative value of primitive data type variables.

–: Unary minus is used when you want to assign a negative value.
+: Unary plus. This is used to convert a negative value to a positive value.
+ +: Increment operator: You can use this operator to increment the value by 1. There are two varieties of increment operators, those are:

  • Post-Increment: In this case, the value is first used for the operator, and after that, the value is updated by 1.
  • Pre-Increment: In this case, the value is first incremented by 1, then its computing use.

 Decrement operator: This acts the same as the increment operator, but where the value is decremented by 1. It also has two varieties, that are:

  • Post-decrement: The current value is used for computing, and later, the value is decremented by 1.
  • Pre-Decrement: Here, the First value is decrement by one later compute is done.

!: Logical not operator: This is used to invert the boolean type value. If the current value is true, the operator inverts that value to false and vice versa.

Program: Unary Operator Example
public class UnaryOperatorsEx 
{
   public static void main(String[] args) 
   {
      int a = 11, b = 22, c = 33, d = 44; 
      boolean condition = true; 

      System.out.println(" Current Value Of A = " + a);
      System.out.println(" Current Value Of B = " + b);
      System.out.println(" Current Value Of C = " + c);
      System.out.println(" Current Value Of D = " + d);

      // pre-increment operator
      System.out.println("Value of A after Pre Increment = " + (++a)); 
      System.out.println(" Current Value Of A = " + a);

      // post increment operator 
      System.out.println("Value of B After Post Increment = " + (b++)); 
      System.out.println(" Current Value Of B = " + b);

      // pre-decrement operator 
      System.out.println("Value of C Pre Decrement = " + (--c)); 
      System.out.println("Current Value of C = " + c); 

      // post-decrement operator 
      System.out.println("Value of D after post decrement = " + (d--)); 
      System.out.println("Current Value of D = " + d); 

      // Logical not operator 
      System.out.println("Value of !condition ="+ !condition); 
   }
}

Output:

 Current Value Of A = 11
 Current Value Of B = 22
 Current Value Of C = 33
 Current Value Of D = 44
Value of A after Pre Increment = 12
 Current Value Of A = 12
Value of B After Post Increment = 22
 Current Value Of B = 23
Value of C Pre Decrement = 32
Current Value of C = 32
Value of D after post decrement = 44
Current Value of D = 43
Value of !condition =false

Arithmetic Operator

These operators perform fundamental arithmetic operations like addition, subtraction, etc., on the primitive data types. There are five arithmetic operators in Java, and here is the list:

  • *: Multiplication
  • /: Division
  • %: Modulo
  • +: Addition
  • –: Subtraction

Since these operators act upon two operands simultaneously, these are called binary operators.

Program: Arithmetic Operator Example
package com.softwaretestingo.enumeration;
public class ArithmaticOperatorEx 
{
	public static void main(String[] args) 
	{
		int a = 11, b = 22; 
		String string1 = "Software", string2 = "Testingo"; 

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

		// + operator if used with strings 
		System.out.println("Use of + Operator With String " + string1 + string2); 

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

		// modulo operator gives remainder 
		// on dividing first operand with second 
		System.out.println("a % b = " + (a % b)); 
	}
}

Output:

a + b = 33
a - b = -11
Use of + Operator With String SoftwareTestingo
a * b = 242
a / b = 0
a % b = 11

Relational Operator In Java

These operators are used to compare two variables. For example, to know which is bigger or whether two quantities are equal. There are six relational operators are available:

  • ==, Equal to:- returns true if both variables have the same value.
  • != Not Equal to returns true if both variables don’t have the same value.
  • <, less than:- returns true of the left-hand side is less than the right-hand side.
  • <=, less than or equal to Returns true of the left-hand side is less than or equal to the right-hand side.
  • > Greater than returns true of the left-hand side is higher than the right-hand side.
  • >=, Greater than or equal to returns true of the left-hand side is greater than or equal to the right-hand side.
Program: Relational Operator Example
package com.softwaretestingo.operators;
public class RelationalOperatorsEx 
{
	public static void main(String[] args) 
	{
		int a = 20, b = 10; 
		String x = "SoftwareTestingo", y = "SoftwareTestingo";
		int array1[] = { 1, 2, 3 }; 
		int array2[] = { 1, 2, 3 }; 
		boolean condition = true; 

		// various conditional operators 
		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)); 

		// Arrays cannot be compared with 
		// relational operators because objects 
		// store references not the value 
		System.out.println("x == y : " + (array1 == array2)); 

		System.out.println("condition==true :"+ (condition == true));  
	}
}

Output:

a == b :false
a < b :false
a <= b :false
a > b :true
a >= b :true
a != b :true
x == y : false
condition==true :true

Logical Operator In Java

Logical operators are used to construct compound conditions. A compound condition is a combination of several simple conditions. Logical operators are of 3 types:

  • && and operator
  • || or operator
  • ! not operator
Program: Logical Operator Example
package com.softwaretestingo.operators;
import java.util.Scanner;
public class LogicalOperatorsEx 
{
	public static void main(String[] args) 
	{
		String uid = "software"; 
		String pwd = "testingo"; 

		Scanner s = new Scanner(System.in); 
		System.out.print("Enter username:"); 
		String uuid = s.next(); 
		System.out.print("Enter password:"); 
		String upwd = s.next(); 

		// Check if user-name and password match or not. 
		if ((uuid.equals(uid) && upwd.equals(pwd)) || (uuid.equals(pwd) && upwd.equals(uid))) 
		{ 
			System.out.println("Welcome user."); 
		} 
		else { 
			System.out.println("Wrong uid or password"); 
		} 
	}
}

Output:

Enter username:software
Enter password:testingo
Welcome user.

Boolean Operator In Java

These operators act on the boolean variables and produce boolean-type results. There are three boolean operators. Are there

  • & boolean and operator
  • | boolean or operator
  • ! boolean not operator

Boolean & operator returns true if both the values of the variables are true. The boolean | operator returns true if any one of the variables is true. Boolean! The operator converts true to false and vice versa.

Bitwise operator In Java

There are seven types of bitwise operators. let’s discuss one by one

  • Bitwise Complement Operator (~): This operator gives the complement form of a given number. This operator symbol is ~, which is pronounced as a tilde.
  • Bitwise and operator (&): This operator operates on the individual bits of the numbers. The symbol for this operator is &, which is called an ampersand.
  • Bitwise Or operator (|): This operator operates on the bits of the numbers. The symbol is |, which is called a pipe symbol.
  • Bitwise Xor operator (^): This operator performs exclusive or (Xor) operations on the bits of the numbers. The symbol is ^, a cap, carat, or circumflex symbol.
  • Bitwise left shift operator(<<): This operator shifts the bits of the number towards the left in a specified number of positions. The symbol for this operator is <<, read as double less than. If we write x<<n, the meaning is to shift the bits of x towards left n positions.
  • Bitwise right shift operator (>>): This operator shifts the bits of the number towards the right in a specified number of positions. The symbol for this operator is >>, read as double greater than. If we write X>>n, the meaning is to shift the bits of x towards the right n positions.
  • Bitwise Zero-fill right shift operator(>>>): This operator also shifts the bits of the number toward the right in a specified number of positions. But it stores 0 in the sign bit. The symbol of this operator is >>>, read as triple greater than. It is called a zero-fill right shift operator since it always fills 0 in the sign bit. If we apply >>> on a positive number, it gives the same output as that of >>. But in the case of negative numbers, the output will be positive since the sign bit is replaced by a 0.

Ternary operator Java or conditional operator (? :):

This operator is called ternary because it acts on three variables. The other name of this operator is a conditional operator since it represents a conditional statement. Two symbols used for this operator? And.

Variable = expressions1 ? expressions2 expressions3;

This means that, first of all, expression 1 was evaluated. If it is true, the expressions2 value is stored in the variable. If expressions1 is false, the expressions3 value is stored in the variable.

Member Operator (.): A member operator is also called a dot operator since its symbol is a. (dot or period). This operator tells about a member of a package or a class. It is used in three ways.

We know that a package contains classes. We can use it — the operator to refer to the package class.
Syntax:

Packagename.classname

We know that each class contains variables and methods. We can use this operator to refer to a class’s variable.
Syntax:

Classname.variablename; or object name.variablename;

We know that a class contains methods. Using the dot operator, we can refer to the methods of a class.
Syntax:

classname.methodname; or objectname.methodname;

The instance of Operator In Java

This operator is used to test whether an object belongs to the class. Note that the word instance means object. This operator can also check whether an object belongs to an interface.

Syntax:

boolean variable = object instanceof class; 
boolean variable = object instanceof interface;

If the object is an instanceof of a class or interface, it returns true. Otherwise, it returns false.

New Operator: A new operator is often used to create objects for classes. We know that JVM dynamically creates objects on heap memory (at runtime).
Syntax:

classname obj= new classname();

Cast Operator:

Cast operators are used to convert one data type to another data type. This operator can be used by writing datatype inside simple braces.

The Priority of Operator In Java

When several operators are used in a statement, knowing which operator will execute first and which will come next is important. To determine that certain rules of operator precedence followed:

  • First, the contents inside the braces () and [] will be executed
  • Next, ++ and —
  • Next, *,/ and % will execute
  • + and – will come next
  • Relational operator and executed next
  • Boolean and bitwise operators
  • Logical operators will come afterward.
  • Then ternary operator
  • Assignment operators are executed at the last

If you find anything wrong or want to share more information on this topic, write to us in the comment section.

Avatar for Softwaretestingo Editorial Board

I love open-source technologies and am very passionate about software development. I like to share my knowledge with others, especially on technology that's why I have given all the examples as simple as possible to understand for beginners. All the code posted on my blog is developed, compiled, and tested in my development environment. If you find any mistakes or bugs, Please drop an email to softwaretestingo.com@gmail.com, or You can join me on Linkedin.

Leave a Comment