Operators

In Java, operators are special symbols that perform specific operations on variables and values. They help you do things like calculations, comparisons, and logic checks in your programs. Java has several types of operators, each serving a different purpose.

Arithmetic Operators

These operators are used to perform basic math operations.

Operator Name Functions Example
+ Addition Add two values x + y
- Subtraction Subtracts a value from another x-y
* Multiplication Multiplies two values x*y
/ Division Divides one value by another x / y
% Modulus Returns the remainder x % y

Example:-

Java
Copy
int a = 10;
int b = 5;
System.out.println("Addition: " + (a + b));  // Output: 15
System.out.println("Substraction: " + (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

These operators compare two values and return a boolean result (true or false).

Operator Name Functions Example
== Equal to Returns true if both the operands are referring to the same object, otherwise false x == y
!= Not equal to Returns false if both the operands are referring to the same object, otherwise true x != y
> Greater than Returns true if value of left operands is greater than right, otherwise false x > y
< Less than Returns false if value of left operands is less than right, otherwise true x < y
>= Greater than and equals Returns true if value of left operands is greater than and equal to right, otherwise false x >= y
<= Less than and equals Returns false if value of left operands is less than and equal to right, otherwise true x <= y

Example:

Java
Copy
int x = 10;
int y = 20;
System.out.println(x > y);  // Output: false
System.out.println(x < y);  // Output: true

Logical Operators

Logical operators are used to combine multiple conditions.

Operator Name Functions Example
&& Logical And Returns true if both the operands evaluates to true, otherwise false x && y
|| Logical OR Returns true if one either one of the operands evaluates to true, otherwise true x || y
! Logical NOT Reverses the result of a condition !(x > y)

Example :

Java
Copy
boolean isJavaFun = true;
boolean isMathHard = false;
System.out.println(isJavaFun && isMathHard);  // Output: false
System.out.println(isJavaFun || isMathHard);  // Output: true
System.out.println(!isJavaFun);               // Output: false

Assignment Operators

Assignment operators are used to assign values to variables.

Operator Description Functions Example
= Assigns a value Assignment x = 10
+= Adds and assigns x = x + 5 x += 5
-= Subtracts and assigns x = x - 5 x -= 5
*= Multiplies and assigns x = x * 5 x *= 5
/= Divides and assigns x = x / 5 x /= 5
%= Modulus and assigns x = x % 5 x %= 5

Example:

Java
Copy
int z = 10;
z += 5;  // z is now 15
z *= 2;  // z is now 30
System.out.println(z);  // Output: 30

Unary Operators

Unary operators operate on a single operand.

Operator Description Functions Example
++ Increases a value by 1. Increment a++
-- Decreases a value by 1. Decrement a--
+ Positive value, usually redundant. Unary plus +a
- Negative value. Unary minus -a

Example:

Java
Copy
int count = 10;
System.out.println(count++);  // Output: 10 (then count becomes 11)
System.out.println(--count);  // Output: 10 (count is decremented before output)

Ternary Operator

The ternary operator is a shorthand way of writing an if-else condition.

Syntax: condition ? valueIfTrue : valueIfFalse

Example:

Java
Copy
int age = 20;
String result = (age >= 18) ? "Adult" : "Minor";
System.out.println(result);  // Output: Adult