Operators in java

There are different types of operators in Java which are classified into different categories based on the type of operation they perform. Operators are used to perform operations on variables and values, and they help us build logic in the program. By using operators, we can do things like mathematical calculations, comparisons, and decision-making in Java.
Definition:
An operator is a symbol that is used to perform some operation on any values.
In Java, we can perform operations at the following three levels: These levels define how many values (operands) are involved in the operation, and each level is used to perform different types of tasks in a program.
Unary operators:
If any operator is given with a single variable or value or expression, then it is called a unary operator.
Ex: -a, +a, …
Binary operators:
If any operator is given with 2 variables or values or expressions, then it is called a binary operator.
Ex: a + b, a - b, a + b + c, …
Ternary operator:
If any operator is given with 3 variables or values or expressions, then it is called a ternary operator.
Ex: condition ? value1 : value2;
Arithmetic Operators
Arithmetic operators are used to perform different mathematical operations.
In Java, we have 5 types of arithmetic operators:
Addition (+)
Subtraction (-)
Multiplication (*)
Division (/) → Quotient
Modulus (%) → Remainder
Assignment Operators
Assignment operators are used to assign or store any value or expression result into the left-side variable. Assignment operator can be specified by using the single =
symbol.
Compound assignment operators do automatic type casting:
a = a + b → a += b
a = a - b → a -= b
a = a * b → a *= b
a = a / b → a /= b
a = a % b → a %= b
Comparison Operators (Relational Operators)
Comparison operators are used to compare any two values or to write conditions in the application.
In Java, we have 6 types of comparison operators:
Less than (<)
Greater than (>)
Less than or equal to (<=)
Greater than or equal to (>=)
Equals to (==)
Not equals to (!=)
Unary Minus
Unary minus operator is used to change the sign of the given number.
Ex:
int a = 10;
a = -a;
System.out.println(a); // -10
Increment and Decrement Operators
Increment or decrement operators are used to increase or decrease the value of a variable by 1.
Pre-increment (++a) → First the value will be increased, then used in the application.
Post-increment (a++) → First the value will be used, then increased.
Pre-decrement (--a) → First the value will be decreased, then used.
Post-decrement (a--) → First the value will be used, then decreased.
Logical Operators
Logical operators are used to combine multiple conditions and also to complement the given condition.
Single AND (&) → checks both conditions but always evaluates both sides.
Double AND (&&) → checks both conditions but evaluates second only if needed.
Single OR (|) → checks both conditions but always evaluates both sides.
Double OR (||) → checks both conditions but evaluates second only if needed.
XOR (^) → returns true if only one condition is true.
NOT (!) → reverses the condition (true → false, false → true).
Ternary Operator (Conditional Operator)
Ternary operator returns the value based on the given condition.
Syntax: condition ? value1 : value2;
new Operator
new is a Java keyword or operator used to create an object and this can be for both user-defined and predefined classes.
The object created using new will be stored inside the heap memory of the JVM.
Syntax: ClassName referencevariable = new ClassName();
Dot (.) Operator
The dot operator is used to access members of the class using an object reference, and it can also be used to refer or identify the class from a package.
In Java, the execution of mathematical expressions follows the BODMAS rule, which means the operations are performed in a specific order: Brackets, Orders (powers), Division, Multiplication, Addition, and Subtraction. This helps to get the correct result when multiple operators are used together in an expression.
Operators are very important in Java because they help us perform different operations, build logic, and control the flow of the program. By learning the types of operators and how to use them correctly, we can write better and more powerful Java programs.
Subscribe to my newsletter
Read articles from PEMMANA VISWESHWAR REDDY directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
