Java operators are special symbols that perform operations on variables and values. They are used to manipulate data and variables in expressions. Java provides a wide array of operators categorized into several types based on their functionality.
Types of Java Operators
Arithmetic Operators
Relational Operators
Logical Operators
Assignment Operators
Unary Operators
Bitwise Operators
Ternary Operator
Arithmetic Operators
Arithmetic operators are used to perform basic mathematical operations.
+ (Addition)
- (Subtraction)
* (Multiplication)
/ (Division)
% (Modulus)
Example
int a =10;
int b =5;
System.out.println("Addition: "+(a + b));// Output: 15
System.out.println("Subtraction: "+(a - b));// Output: 5
System.out.println("Multiplication: "+(a * b));// Output: 50
System.out.println("Division: "+(a / b));// Output: 2
System.out.println("Modulus: "+(a % b));// Output: 0рд╕рдВрдЪрд╛рд▓рд┐рдд рджреНрд╡рд╛рд░рд╛
Relational Operators
Relational operators are used to compare two values.
== (Equal to)
!= (Not equal to)
> (Greater than)
< (Less than)
>= (Greater than or equal to)
<= (Less than or equal to)
Example
int a =10;
int b =5;
System.out.println(a == b);// Output: false
System.out.println(a > b);// Output: true
System.out.println(a < b);// Output: falseрд╕рдВрдЪрд╛рд▓рд┐рдд рджреНрд╡рд╛рд░рд╛
Logical Operators
Logical operators are used to combine multiple boolean expressions.
&& (Logical AND)
|| (Logical OR)
! (Logical NOT)
Example
boolean x =true;
boolean y =false;
System.out.println(x && y);// Output: false
System.out.println(x || y);// Output: true
System.out.println(!x);// Output: falseрд╕рдВрдЪрд╛рд▓рд┐рдд рджреНрд╡рд╛рд░рд╛
Assignment Operators
Assignment operators are used to assign values to variables.
Java operators are special symbols that perform operations on variables and values. They are used to manipulate data and variables in expressions. Java provides a wide array of operators categorized into several types based on their functionality.
Types of Java Operators
Arithmetic Operators
Arithmetic operators are used to perform basic mathematical operations.
+(Addition)-(Subtraction)*(Multiplication)/(Division)%(Modulus)Example
Relational Operators
Relational operators are used to compare two values.
==(Equal to)!=(Not equal to)>(Greater than)<(Less than)>=(Greater than or equal to)<=(Less than or equal to)Example
Logical Operators
Logical operators are used to combine multiple boolean expressions.
&&(Logical AND)||(Logical OR)!(Logical NOT)Example
Assignment Operators
Assignment operators are used to assign values to variables.
=(Simple assignment)+=(Add and assign)-=(Subtract and assign)*=(Multiply and assign)/=(Divide and assign)%=(Modulus and assign)Example
Unary Operators
Unary operators operate on a single operand.
+(Unary plus)-(Unary minus)++(Increment)--(Decrement)Example
Bitwise Operators
Bitwise operators perform operations on bits.
&(Bitwise AND)|(Bitwise OR)^(Bitwise XOR)~(Bitwise NOT)<<(Left shift)>>(Right shift)>>>(Unsigned right shift)Example
Ternary Operator
The ternary operator is a shorthand form of the
if-elsestatement.? :(Ternary)Example
Tips and Best Practices