They allow you to manipulate and perform computations on data. Java provides a wide range of operators categorized into different types: arithmetic, assignment, comparison, logical, bitwise, and more. Here’s a detailed explanation of each type with syntax examples:
What are Operators in JAVA?
Operators in programming are symbols or special characters that enable you to perform various actions and computations on data. They act as tools that manipulate and combine values in meaningful ways. For example, arithmetic operators such as addition (+), subtraction (-), multiplication (*), and division (/) allow you to perform basic mathematical calculations.
Comparison operators like equal to (==) or less than (<) help you compare values and make decisions based on the results. Logical operators such as AND (&&) or OR (||) allow you to combine conditions and control the flow of your program. Assignment operators (e.g., =, +=, -=)) also assign values to variables. Bitwise operators manipulate individual bits in binary numbers. Understanding and utilizing operators is essential in programming.
They enable you to create complex algorithms, perform calculations, and make decisions based on conditions, making your programs more dynamic and powerful. if you want to learn Java developer course, you can register yourself
Recommended Course
- Decode DSA with C++
- Full Stack Data Science Pro Course
- Java For Cloud Course
- Full Stack Web Development Course
- Data Analytics Course
Types of Operators in JAVA
1. Arithmetic Operators
Arithmetic operators in Java are symbols that perform mathematical calculations on numeric data types. Java provides several arithmetic operators:
- Arithmetic Operators:
- Addition (+): Adds two operands.
- Subtraction (-): Subtracts the second operand from the first.
- Multiplication (*): Multiplies two operands.
- Division (/): Divides the first operand by the second.
- Modulus (%): Returns the remainder of the division operation
- Comparison Operators: These operators compare two values and return a boolean result (true or false). For example, the equal to operator (==) checks if two values are equal, the not equal to operator (!=) checks if two values are not equal, the greater than operator (>) checks if one value is greater than another, the less than operator (<) checks if one value is less than another, and the greater than or equal to operator (>=) and the less than or equal to operator (<=) check for greater than or equal to and less than or equal to conditions, respectively.
- Logical Operators: These operators are used to combine or negate boolean expressions. The logical AND operator (&&) returns true if both expressions are true on their left and right sides. The logical OR operator (||) returns true if at least one of the expressions on its left or right sides is true. The logical NOT operator (!) reverses the logical state of an expression.
- Bitwise Operators: These operators perform operations on individual bits of binary numbers. They are used in low-level programming or require direct bit manipulation.
Understanding and utilizing these operators is crucial for writing effective and meaningful Java programs, as they allow you to perform various operations, comparisons, assignments, and logical evaluations on different types of data.
Mathematical operations on numeric operands. You can use them to do addition, subtraction, multiplication, division, and modulus operations.
Operators | Descriptions |
Add ( + ) | Return Addition of two Numbers |
Subtract ( -) | Returns Subtraction of Two Numbers |
Multiply ( * ) | Returns Multiplication of Two Numbers |
Divide ( / ) | Returns Division of Two Numbers |
Modulus ( % ) | Returns remainder after dividing Two Numbers |
SYNTAX:
public class Operators_PWskills {
public static void main(String args[])
{
int num1=20;
int num2=2;
System.out.println(num1+num2); //Output – 22
System.out.println(num1-num2); //Output – 20
System.out.println(num1*num2); //Output – 40
System.out.println(num1/num2); //Output – 10
System.out.println(num1%num2); //Output – 0
}
}
2. Unary Operators in Java
These operators are called unary operators because they need a single operand at a time. Unary Operators are used to perform increment, decrement, negation, etc, operations on operands.
- Unary minus (-) : This is used to increment an integer’s value or convert a negative integer into a positive integer.
SYNTAX:
A = -20
This is subtracted from value 20 from A.
- Unary add (+): Used to increment the values of an integer. There are two types of increment in the Unary add operator.
- Post-Increment Operator – When the increment is placed after the operator. This means the value is not incremented instantly. 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.
- Pre-Increment Operator – When the increment is placed before the operator. This means the value is incremented instantly. This operator will not wait for the execution of the statement, and it automatically increments the value.
- Unary Not (!) Operator: It is used to convert boolean values. It converts true into false and vice-versa.
- Bitwise Complement: It returns One’s complement of input operand with all the bits converted.
SYNTAX:
~(operand)
For example,
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
3. Assignment Operators in Java
In Java programming language, a group of assignment operators gives variable values. By placing the variable on the left side of the operator and the value or expression on the right, you may use them to store a value in a variable. Assignment operators can combine values with other operations simply in addition to assigning values.
SYNTAX:
public class OperatorExample {
public static void main(String[] args) {
int num = 15;
num+=5; //15+5 = 20
System.out.println(num);
num-=4; // 20-4 = 16
System.out.println(num);
num*=2; //16*2 = 32
System.out.println(num);
num/=2; //32/2 = 16
System.out.println(num);
}
}
4. Relational Operators –
Relational operators are a group of binary operators that are used to find relations and compare the relationships between two operands or numbers. These operators include equality, greater than, less than, and others. After the comparison, they provide a boolean result and are frequently used in looping statements and conditional if-else expressions.
Equal to (=) Operator:
The two operands of this operator are compared to see if they are equal. The operator returns true if the operands on the left and right are equal; otherwise, it returns false.
Syntax:
var1 = “PWSkills”
var2 = “Science”
var1 == var2 results in false
Not Equal to (!=) Operator:
This operator is used to specify whether two operands are equal or not. It works in the background from an identical operator. If the left and right operators are unequal, then true otherwise, False.
Syntax:
var1 = “PWSkills”
var2 = “Science”
var1 != var2 results in true
Greater than (>) Operator
The value of the first operand is checked if it is greater than the second operand. If the operand on the left is greater than the operand on the right, the operator returns true.
Syntax:
var1 = 35
var2 = 20
var1 > var2 results in true
Less than (<) Operator
The value of the first operand is checked to see if it is smaller than the second operand. If the operand on the left is less than the operand on the right, the operator returns true.
Syntax:
var1 = 35
var2 = 20
var2 < var1 results in False
Greater than or equal to (>=)
determines if the first operand exceeds or is equal to the second operand. The operator returns true when the operand on the left is larger than or equal to the operand on the right.
Syntax:
var1 = 30
var2 = 30
var3 = 10
var1 >= var2 results in true
var2 >= var3 results in true
Less than or equal to (<=)
determines if the first operand is less or equal to the second operand. The operator returns true when the operand on the left is smaller than or equal to the operand on the right.
Syntax:
var1 = 30
var2 = 30
var3 = 40
var1 <= var2 results in true
var2 <= var3 results in true
Frequently Asked Questions
- What do Java operators do?
Ans. A special symbol known as an operator instructs the compiler to carry out particular logical or mathematical operations. It is typically used in programs to carry out a certain operation on operands.
- What is a Binary operator in Java?
Ans. An operator that acts on two operands is called a binary operator. It uses two variables.
- What are the types of arithmetic operators?
Ans. Java programming supports five types of arithmetic operators. They are:
- + Addition
- – Subtraction
- * Multiplication
- / Division
- % Modulo division (Remainder)
4. What do Java’s relational operators do?
Ans. In Java, relational operators compare two numerical values or two quantities. They are often used to establish conditions in looping and branching statements.
- What does Java’s unary operator mean?
Ans. A unary operator is one that only affects a single operand. It just uses one variable.
Recommended Reads
Data Science Interview Questions and Answers
Data Science Internship Programs