In this blogging you know about digital world.
- Get link
- X
- Other Apps
Operators in Java

Operators in Java are symbols that perform operations on variables and values. They allow you to manipulate data and perform calculations in your Java programs. Java provides a variety of operators, which can be categorized into several groups:
1. **Arithmetic Operators:**
- `+` (Addition): Adds two operands.
- `-` (Subtraction): Subtracts the right operand from the left operand.
- `*` (Multiplication): Multiplies two operands.
- `/` (Division): Divides the left operand by the right operand.
- `%` (Modulus): Returns the remainder of the division of the left operand by the right operand.
Example:
int a = 10, b = 5;
int sum = a + b; // sum is 15
int difference = a - b; // difference is 5
int product = a * b; // product is 50
int quotient = a / b; // quotient is 2
int remainder = a % b; // remainder is 0
2. **Assignment Operators:**
- `=` (Assignment): Assigns a value to a variable.
- `+=`, `-=`, `*=`, `/=`, `%=` (Compound Assignment): Perform an operation and assign the result to the left operand.
Example:
int x = 5;
x += 3; // Equivalent to x = x + 3; x is now 8
3. **Comparison Operators:**
- `==` (Equality): Tests if two values are equal.
- `!=` (Inequality): Tests if two values are not equal.
- `<` (Less Than): Tests if the left operand is less than the right operand.
- `>` (Greater Than): Tests if the left operand is greater than the right operand.
- `<=` (Less Than or Equal To): Tests if the left operand is less than or equal to the right operand.
- `>=` (Greater Than or Equal To): Tests if the left operand is greater than or equal to the right operand.
Example:
int a = 5, b = 10;
boolean isEqual = (a == b); // isEqual is false
boolean isNotEqual = (a != b); // isNotEqual is true
boolean isLessThan = (a < b); // isLessThan is true
4. **Logical Operators:**
- `&&` (Logical AND): Returns true if both operands are true.
- `||` (Logical OR): Returns true if at least one operand is true.
- `!` (Logical NOT): Inverts the value of the operand.
Example:
boolean x = true, y = false;
boolean result1 = x && y; // result1 is false
boolean result2 = x || y; // result2 is true
boolean result3 = !x; // result3 is false
5. **Bitwise Operators:**
- `&` (Bitwise AND): Performs a bitwise AND operation.
- `|` (Bitwise OR): Performs a bitwise OR operation.
- `^` (Bitwise XOR): Performs a bitwise XOR (exclusive OR) operation.
- `~` (Bitwise NOT): Inverts the bits of the operand.
- `<<` (Left Shift): Shifts the bits to the left.
- `>>` (Right Shift): Shifts the bits to the right.
- `>>>` (Unsigned Right Shift): Shifts the bits to the right with zero fill.
Example:
int a = 5, b = 3;
int result1 = a & b; // result1 is 1 (binary: 0101 & 0011 = 0001)
int result2 = a | b; // result2 is 7 (binary: 0101 | 0011 = 0111)
int result3 = a ^ b; // result3 is 6 (binary: 0101 ^ 0011 = 0110)
6. **Conditional (Ternary) Operator:**
- `? :` (Conditional Operator): A shorthand way to express an if-else statement.
Example:
int x = 5, y = 10;
int result = (x > y) ? x : y; // result is 10
7. **Instanceof Operator:**
- `instanceof`: Used to test if an object is an instance of a particular class or interface.
Example:
Object obj = new String("Hello");
boolean isString = obj instanceof String; // isString is true
These are the main categories of operators in Java. Understanding how to use them is crucial for writing effective and efficient Java programs.
- Get link
- X
- Other Apps
Popular Posts
Artificial intelligence
- Get link
- X
- Other Apps
Datatype Programming in C, Data Type
- Get link
- X
- Other Apps
Computer Virus Describe in Hindi
- Get link
- X
- Other Apps
Getting Started C Programming
- Get link
- X
- Other Apps
In C, variable names must adhere to certain rules and conventions. Here are some examples of valid and invalid variable names and the reasons why:
- Get link
- X
- Other Apps
Introduction to Java, History and Evolution of Java
- Get link
- X
- Other Apps
5 Versatile programming language for Hacking in 2023
- Get link
- X
- Other Apps
Decision Control Structure in C, Boolean Operator and Expression
- Get link
- X
- Other Apps
Comments
Post a Comment