Operators in Java

Operators in Java are symbols that perform operations on variables and values. They simplify calculations, comparisons, and logical decisions. Let’s explore them with clarity!

Arithmetic Operators

  • Addition (+): Adds two operands.
  • Subtraction (-): Subtracts the second operand from the first operand.
  • Multiplication (*): Multiplies two operands.
  • Division (/): Divides the first operand by the second operand.
  • Modulus (%): Returns the remainder of the division of the first operand by the second operand.

Example:

int result = 15 % 4; // Remainder = 3  
double avg = (10 + 20) / 2.0; // 15.0  

Assignment Operators

  • Assignment (=): Assigns the value on the right to the variable on the left.
  • Compound Assignment Operators (e.g., +=, -=, *=, /=, %=): Combines an arithmetic operation with assignment.

Example:

int x = 5;  
x *= 3; // x becomes 15  

Increment and Decrement Operators

  • Increment (++): Increases the value of a variable by 1.
  • Decrement (–): Decreases the value of a variable by 1.

Example:

int x = 5;  
int a = x++; // a = 5, x becomes 6  
int b = ++x; // b = 7, x becomes 7  

Relational (Comparision) Operators

  • Equal to (==): Checks if two operands are equal.
  • Not equal to (!=): Checks if two operands are not equal.
  • Greater than (>): Checks if the left operand is greater than the right operand.
  • Less than (<): Checks if the left operand is less than the right operand.
  • Greater than or equal to (>=): Checks if the left operand is greater than or equal to the right operand.
  • Less than or equal to (<=): Checks if the left operand is less than or equal to the right operand.

Example:

boolean isEqual = (5 == 5); // true  
boolean isAdult = age >= 18;  

Logical Operators

  • Logical AND (&&): Returns true if both operands are true.
  • Logical OR (||): Returns true if at least one of the operands is true.
  • Logical NOT (!): Reverses the logical state of the operand.

Example:

boolean isEligible = (age > 18) && (hasLicense);  

Bitwise Operators

  • Bitwise AND (&): Performs a bitwise AND operation between two operands.
  • Bitwise OR (|): Performs a bitwise OR operation between two operands.
  • Bitwise XOR (^): Performs a bitwise exclusive OR operation between two operands.
  • Bitwise NOT or Complement (~): Inverts the bits of the operand.
  • Left Shift (<<): Shifts the bits of the left operand to the left by a specified number of positions.
  • Right Shift (>>): Shifts the bits of the left operand to the right by a specified number of positions.
  • Unsigned Right Shift (>>>): Shifts the bits of the left operand to the right by a specified number of positions, filling the leftmost bits with zeros.

Example:

int a = 5; // 0101 in binary  
int b = 3; // 0011  
int c = a & b; // 0001 (1 in decimal)  

Conditional (Ternary) Operator

Ternary Operator (condition ? expr1 : expr2): Evaluates the condition and returns expr1 if true, otherwise returns expr2.

Example:

String status = (marks >= 40) ? "Pass" : "Fail";  

Conclusion

Java operators streamline coding tasks, from math to logic. By understanding their types and rules, you’ll write cleaner, more efficient programs.

Sharing Is Caring:
Subscribe
Notify of
0 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments