OBJECT ORIENTED PROGRAMMING THROUGH JAVA : Unit - 1 : Introduction to Operators and Types of Operators in Java
- Get link
- X
- Other Apps
Operators in Java – Complete Detailed Notes
Operators in Java are special symbols used to perform operations on variables and values. They are essential for building expressions and controlling logic.
Classification of Java Operators
Java operators are broadly categorized into the following:
-
Arithmetic Operators
-
Relational (Comparison) Operators
-
Logical Operators
-
Assignment Operators
-
Unary Operators
-
Bitwise Operators
-
Ternary Operator
-
Shift Operators
-
Instanceof Operator
-
Type Cast Operator
1. Arithmetic Operators
Used to perform basic mathematical operations.
Operator | Description | Example | Result |
---|---|---|---|
+ | Addition | a + b | Sum of a and b |
- | Subtraction | a - b | Difference of a and b |
* | Multiplication | a * b | Product of a and b |
/ | Division | a / b | Quotient |
% | Modulus (Remainder) | a % b | Remainder of a / b |
Example:
int a = 10, b = 3;
System.out.println(a + b); // Output: 13
System.out.println(a % b); // Output: 1
Example program
public class ArithmeticDemo {
public static void main(String[] args) {
int a = 15, b = 4;
System.out.println("Addition: " + (a + b)); // 19
System.out.println("Subtraction: " + (a - b)); // 11
System.out.println("Multiplication: " + (a * b)); // 60
System.out.println("Division: " + (a / b)); // 3
System.out.println("Modulus: " + (a % b)); // 3
}
}
2. Relational (Comparison) Operators
Used to compare two values. Returns a boolean (true
or false
).
Operator | Description | Example |
---|---|---|
== | Equal to | a == b |
!= | Not equal to | a != b |
> | Greater than | a > b |
< | Less than | a < b |
>= | Greater than or equal | a >= b |
<= | Less than or equal | a <= b |
Example Program
3. Logical Operators
Used to combine multiple boolean expressions.
Operator | Description | Example |
---|---|---|
&& | Logical AND | a > 5 && b < 10 |
` | ` | |
! | Logical NOT | !(a == b) |
Example Program
public class LogicalDemo {
public static void main(String[] args) {
int a = 5, b = 10;
boolean result1 = (a < b) && (b > 5);
boolean result2 = (a > b) || (b == 10);
boolean result3 = !(a == b);
System.out.println("AND: " + result1); // true
System.out.println("OR: " + result2); // true
System.out.println("NOT: " + result3); // true
}
}
4. Assignment Operators
Used to assign values to variables.
Operator | Description | Example |
---|---|---|
= | Assign | a = 10 |
+= | Add and assign | a += 5 → a = a + 5 |
-= | Subtract and assign | a -= 3 |
*= | Multiply and assign | a *= 2 |
/= | Divide and assign | a /= 4 |
%= | Modulus and assign | a %= 3 |
Example Program
5. Unary Operators
Operate on a single operand.
Operator | Description | Example |
---|---|---|
+ | Unary plus | +a |
- | Unary minus | -a |
++ | Increment | ++a , a++ |
-- | Decrement | --a , a-- |
! | Logical complement | !true → false |
Pre-increment and Post-increment:
int a = 5;
System.out.println(++a); // 6 (pre-increment)
System.out.println(a++); // 6 (post-increment, then becomes 7)
Example Programpublic class UnaryDemo {
public static void main(String[] args) {
int x = 5;
System.out.println("x: " + x); // 5
System.out.println("++x: " + ++x); // 6
System.out.println("x++: " + x++); // 6 (then x becomes 7)
System.out.println("--x: " + --x); // 6
System.out.println("x--: " + x--); // 6 (then x becomes 5)
System.out.println("Final x: " + x); // 5
}
}
6. Bitwise Operators
Used to perform operations on bits.
Operator | Description | Example |
---|---|---|
& | Bitwise AND | a & b |
` | ` | Bitwise OR |
^ | Bitwise XOR | a ^ b |
~ | Bitwise complement | ~a |
Example Program
Also known as the conditional operator. It's a shortcut for if-else
.
Syntax:
condition ? expression1 : expression2
Example:
int a = 10, b = 20;int max = (a > b) ? a : b; // max = 20
Example Program
public class TernaryDemo {
public static void main(String[] args) {
int a = 10, b = 20;
int max = (a > b) ? a : b;
System.out.println("Maximum is: " + max); // 20
}
}
8. Shift Operators
Used to shift bits left or right.
Operator | Description | Example |
---|---|---|
<< | Left shift | a << 2 |
>> | Right shift | a >> 2 |
>>> | Unsigned right shift | a >>> 2 |
Example Program
9. instanceof Operator
Checks if an object is an instance of a specific class or subclass.
String s = "Hello";
System.out.println(s instanceof String); // true
Example Program
public class InstanceofDemo {
public static void main(String[] args) {
String s = "Hello";
System.out.println(s instanceof String); // true
}
}
10. Type Cast Operator
Used to convert one data type into another.
Example:
int a = 10;
double b = (double) a; // Type casting int to double
Example Program
public class TypeCastDemo {
public static void main(String[] args) {
int a = 10;
double d = (double) a;
System.out.println("Converted to double: " + d); // 10.0
}
}
Summary Table of Java Operators
Type
Operators
Arithmetic
+
, -
, *
, /
, %
Relational
==
, !=
, >
, <
, >=
, <=
Logical
&&
, `
Assignment
=
, +=
, -=
, *=
, /=
, %=
Unary
+
, -
, ++
, --
, !
Bitwise
&
, `
Shift
<<
, >>
, >>>
Ternary
condition ? trueVal : falseVal
instanceof
object instanceof ClassName
Type Cast
(targetType) value
Type | Operators |
---|---|
Arithmetic |
|
Relational |
|
Logical |
|
Assignment |
|
Unary |
|
Bitwise |
|
Shift |
|
Ternary |
|
instanceof |
|
Type Cast |
|
- Get link
- X
- Other Apps
Comments
Post a Comment