Java Fundamentals

Operators in Java

by Passion2Code

Operators in Java are the building blocks that allow you to perform tasks like calculations, comparisons, and logical decisions. They play a key role in making your code functional and efficient. Understanding how operators work and their different types is essential for writing clear, effective Java programs. From basic arithmetic to complex logic, operators are tools every Java programmer relies on.

Operators in Java are the building blocks that allow you to perform tasks like calculations, comparisons, and logical decisions.

Operators in Java


Let’s start by understanding the basic concepts of Expression, operands, and operators.

What is Expression, Operand, and Operator?

  • Operand: An Operand is a data or a value or a variable that is used to perform a task(Operation).
  • Operator: An Operator is a symbol or a keyword that will perform a Task (Operation) on one or more Operands.
  • Expression: The combination of operand and operator that produces a result is known as an Expression or The combination of data and symbol which produces a result is known as an Expression.

For example:

int result = 5 + 3;

  • In this example, 5 and 3 are operands,  + is the operator, and “int result = 5 + 3;” is an Expression. 
  • The operator adds the two operands and the result is stored in the variable result.

Types of Operators

These are symbols that will perform a task, there are three types of Operators.

  1. Unary Operator
    • These are the Symbols that accept only one Data/Operand to perform a task.
  2. Binary Operator
    • These are the symbols that accept only two Data/Operands to perform a task.
  3. Ternary Operator
    • These are the symbols that accept more than two Data/Operands to perform a task.

Java Operators: 7 Essential Types You Need to Know

There are several types of Operators in Java that can be used to perform different kinds of operations. Here’s the list of operators we will discuss.

  1. Arithmetic Operators
  2. Relational Operators
  3. Comparison Operators
  4. Logical Operators
  5. Assignment Operators
  6. Increment/Decrement Operators
  7. Ternary Operators
1. Arithmetic operators

Arithmetic Operators are used to perform basic mathematical operations like addition, subtraction, multiplication, and division. These operators are essential for calculation in any programming task.

OperatorDescriptionExample
+Additiona + b
Subtractiona – b
*Multiplicationa * b
/Divisiona / b
%Modulus (remainder)a % b

These are the automatic operators that are widely used in calculations, from simple mathematics to complex mathematical expressions.

Example:

public class Passion2Code 
{  
    public static void main(String[] args) 
    {
		// Declaring and initializing the variables
		int a = 10, b = 5; 
		int sum = a + b;   // Addition: 15 
		int diff = a - b;  // Subtraction: 5 
		int prod = a * b;  // Multiplication: 50 
		int quot = a / b;  // Division: 2 
		int rem = a % b;   // Modulus: 0
        
    }
}

What is a Condition?

  • An expression that results in either true or false is known as a Condition.
  • Any arithmetic operator will not be a Condition.
2. Relational operators

These are the binary operators used to compare values based on the relationship. They return a boolean value (true or false) based on whether the condition is satisfied. These operators are commonly used in the decision-making process.

OperatorDescriptionExample
>Greater thana > b
<Less thana < b
>=Greater than or equal toa >= b
<=Less than or equal toa <= b

These are the Relational operators that are crucial for implementing logic in programs where conditions need to be checked, such as user age verification or comparing scores in a game.

Example:

public class Passion2Code 
{  
    public static void main(String[] args) 
    {
		int x = 10, y = 5;
		// Greater than: true
		boolean greater = (x > y); 
		// Less than: false    
		boolean less = (x < y);        
		// Greater than or equal to: true
		boolean greaterEqual = (x >= y);    		
		// Less than or equal to: false      
		boolean lessEqual = (x <= y);      
	}
}

3. Comparison operators

These are binary operators, which are used to check for equality or inequality between two values. Unlike relational operators, the focus is exclusively on determining whether values are the same or different.

OperatorDescriptionExample
==Equal toa == b
!=Not equal toa != b

These Comparison operators are commonly using scenarios, such as verifying login credentials or validating input data.

Example:

Data Types in Java
Data Types in Java
public class Passion2Code 
{  
    public static void main(String[] args) 
    {
		int x = 10, y = 5; 
		boolean isEqual = (x == y); // Equal to: false 
		boolean notEqual = (x != y); // Not equal to: true       
    }
}

4. Logical operators

These are the binary operators, which make it easy to combine multiple conditions. They’re particularly useful in complexation decision-making processes where more than one condition used to be evaluated.

OperatorDescriptionExample
&&Logical ANDa > b && b > c
||Logical ORa > b || b > c
!Logical NOT!condition

These operators make it easier to handle multiple conditions in a concise and readable manner.

Example:

public class Passion2Code 
{  
    public static void main(String[] args) 
    {
		boolean condition1 = true;
		boolean condition2 = false;

		//Logical AND: false
		boolean andResult = condition1 && condition2;  
		//Logical OR: true
		boolean orResult = condition1 || condition2;   
		//Logical NOT: false
		boolean notResult = !condition1;		    
    }
}

5. Assignment operators

Assignment operators are used to assign values to variables. They can perform operations like addition or subtraction in combination with assignments.

OperatorDescriptionExample
=Assigna = 10;
+=Add and assigna += 5;
-=Subtract and assigna -= 5;
*=Multiply and assigna *= 5;
/=Divide and assigna /= 5;
%=Modulus and assigna %= 5;

These are the assignment operators with simplify the code when performing operations on the same variable.

Example:

public class Passion2Code 
{  
    public static void main(String[] args) 
    {
		  int c = 10// Simple assignment
		  c += 5// Add AND assignment (c = c + 5): 15
		  c -= 3// Subtract AND assignment (c = c - 3): 12
		  c *= 2;     // Multiply AND assignment (c = c * 2): 24
		  c /= 4;     // Divide AND assignment (c = c / 4): 6
		  c %= 4;     // Modulus AND assignment (c = c % 4): 2
    }
}

6. Increment/Decrement operators

These are originally operators, which are used to increase or decrease a variable’s value by 1. They are commonly used in loops and iterative processes.

OperatorDescriptionExample
++Increment++a or a++
Decrement–a or a–

These Increment/Decrement operators are especially useful in loops, where variables are updated repeatedly.

Pre-Increment and Pre-Decrement:

In the pre-increment (++a) or pre-decrement (–a), the value of the variable is changed before it is used in the expression.

Example:

public class Passion2Code 
{  
    public static void main(String[] args) 
    {
		int value = 5;
		++value; // Pre-increment: increase then use value
		--value; // Pre-decrement: decrease then use value		      
    }
}

Post-Increment and Post-Decrement:

In the post-increment (a++) or post-decrement (a–), the value of the variable is used in the expression first, and then it is changed.

Example:

public class Passion2Code 
{  
    public static void main(String[] args) 
    {
		int value = 5;
		value++; // Post-increment: use value then increase 
		value--; // Post-decrement: use value then decrease 	      
    }
}

7. Ternary Operator

The ternary operator is a concise way to express conditional statements. It evaluates a boolean condition and returns one of two values based on whether the condition is true or false.

Syntax: variable = (condition) ? expression1 : expression2;

Variables in Java are like containers that store data values, making them essential for performing calculations, holding temporary results, and enabling dynamic application behavior.
Variables in Java

The ternary operator a shorthand way of writing if-else statements, simplifies simple if-else logic into a single line.

Example:

public class Passion2Code 
{  
    public static void main(String[] args) 
    {
		int a = 10, b = 5;
		// If a > b, max = a, else max = b
		int max = (a > b) ? a : b;  	      
    }
}

Summary

  • Operators are essential in Java programming, allowing developers to carry out a wide range of operations effectively.
  • By grasping their different types and applications, you can create cleaner and more efficient code.
  • Whether you’re performing calculations, making decisions, or assigning values, operators are crucial to programming logic.
  • Gaining a solid understanding of these concepts will enable you to develop strong and dependable applications.

Must Read

JDK – Java Development Kit
Discover why JDK is essential for Java development.

Execution of Java Program
Follow the step-by-step process of how a Java program is executed.

Structure of Java Program – Java Syntax
Understand the basic structure and syntax of a Java program.

Data Types in Java
Explore Java’s data types and how they are used to define variables.

Variables in Java
Learn how to declare and use variables in Java effectively.


FAQ’S

What is an operator in Java?
An operator is a symbol or keyword used to perform operations on operands. For example, + adds two values.

What are operands in Java?
Operands are the values or variables on which operators perform actions. In a + b, a and b are operands.

What is a unary operator?
A unary operator works on a single operand. Example: ++a increments a by 1.

What is the difference between == and =?
= is an assignment operator, while == is a comparison operator used to check equality.

What is the ternary operator in Java?
The ternary operator is a shorthand for if-else statements. Example: (a > b) ? a : b returns the greater value.

What are arithmetic operators in Java?
Arithmetic operators include +, , *, /, and %, used for mathematical calculations.

How does the logical AND (&&) work?
The && operator returns true if both conditions are true. Example: a > 0 && b > 0.

What is the use of assignment operators in Java?
Assignment operators assign values to variables and combine operations like addition or multiplication.

What is the difference between relational and comparison operators in Java?
Relational operators compare values based on their relationship (e.g., greater than, less than), while comparison operators check for equality or inequality (e.g., equal to, not equal to).

Leave a Comment

Index