Java Operators
- Java operators are special symbols used to perform operations on variables and values.
- Operators are used for calculations, comparisons, logical decisions, and assigning values.
- Java provides different types of operators such as arithmetic, relational, logical, assignment, unary, bitwise, and ternary operators.
- Operators make programs easier to write by performing different operations using simple symbols.
- Every operator works with one or more operands to produce a result.
What are Operators in Java?
Operators in Java are symbols that tell the compiler to perform a specific operation on one or more values.
The values on which operators perform operations are called operands.
For example, in the expression 10 + 20, the plus (+) symbol is an operator and 10 and 20 are operands.
Java operators are commonly used in mathematical calculations, decision-making statements, loops, and data manipulation. Understanding operators is an important part of learning Java programming.
Types of Java Operators
Java provides several types of operators to perform different operations. The main types of Java operators are:
Arithmetic Operators
Used to perform mathematical operations like addition, subtraction, multiplication, division, and modulus.
Assignment Operators
Used to assign values to variables.
Relational Operators
Used to compare two values and return a boolean result.
Logical Operators
Used to combine multiple conditions.
Unary Operators
Used with a single operand to perform operations like increment and decrement.
Ternary Operator
Used as a short form of if-else statements.
Arithmetic Operators in Java
Arithmetic operators are used to perform basic mathematical calculations. These operators work with numeric values such as int, float, and double.
public class Main{
public static void main(String[] args) {
int a =20;
int b =10;
System.out.println(a + b);
System.out.println(a - b);
System.out.println(a * b);
System.out.println(a / b);
System.out.println(a % b);
}
}
30 10 200 2 0
Arithmetic Operators List
+
Addition operator is used to add two values.
-
Subtraction operator is used to subtract one value from another.
*
Multiplication operator is used to multiply values.
/
Division operator divides one value by another.
%
Modulus operator returns the remainder after division.
Assignment Operators in Java
Assignment operators are used to assign values to variables. The most common assignment operator is the equal (=) operator.
int number = 50;
number += 10;
System.out.println(number);
Relational Operators in Java
Relational operators are used to compare two values. The result of relational operators is always true or false.
int a = 10;
int b = 20;
System.out.println(a < b);
System.out.println(a > b);
true false
Logical Operators in Java
Logical operators are used to combine two or more conditions.
These operators are mainly used with decision-making statements like
if, if-else, and loops.
The result of logical operators is always a boolean value (true or false).
int age = 20;
System.out.println( age > 18 && age < 30);
System.out.println( age < 18 || age > 30);
true false
Logical Operators List
&&
Logical AND operator returns true when both conditions are true.
||
Logical OR operator returns true when at least one condition is true.
!
Logical NOT operator reverses the result. True becomes false and false becomes true.
Unary Operators in Java
Unary operators are operators that work with only one operand. These operators are commonly used for increasing or decreasing values and changing the sign of a number.
int number = 10; number++;
System.out.println(number);
number--;
System.out.println(number);
11 10
Unary Operators List
++
Increment operator increases the value of a variable by 1.
--
Decrement operator decreases the value of a variable by 1.
+
Unary plus operator represents a positive value.
-
Unary minus operator converts a positive value into negative.
Bitwise Operators in Java
Bitwise operators are used to perform operations on individual bits of integer values. These operators work directly with binary representation of numbers.
int a =5;
int b = 3;
System.out.println(a & b);
System.out.println(a | b);
1 7
Bitwise Operators List
&
Performs bitwise AND operation between two numbers.
|
Performs bitwise OR operation between two numbers.
^
Performs bitwise XOR operation.
~
Performs bitwise NOT operation.
Ternary Operator in Java
The ternary operator is a short form of the if-else statement. It is also known as the conditional operator because it checks a condition and returns one of two values.
int age =20;
String result = (age >= class="cx">18)?
Adult" : "Not Adult";
System.out.println(result);
Java Operator Precedence
Operator precedence defines the order in which operators are evaluated when multiple operators are used in a single expression. Operators with higher precedence are executed first.
int result = 10 +5 *2;
System.out.println(result);
Conclusion
Java operators are an essential part of programming because they allow developers to perform calculations, compare values, assign data, and create logical conditions. Understanding different types of operators helps programmers write efficient and meaningful Java programs.