Java Operators

Java Operators

Operators in Java are special symbols used to perform operations on variables and values.

Java provides several types of operators:


🔹 1. Arithmetic Operators

Used to perform mathematical operations.

Operator Name Example Result
+ Addition 10 + 5 15
- Subtraction 10 - 5 5
* Multiplication 10 * 5 50
/ Division 10 / 5 2
% Modulus (Remainder) 10 % 3 1

Example:

public class Main {
public static void main(String[] args) {
int a = 20, b = 6;
System.out.println(a + b);
System.out.println(a - b);
System.out.println(a * b);
System.out.println(a / b);
System.out.println(a % b);
}
}

🔹 2. Assignment Operators

Used to assign values to variables.

Operator Meaning Example
= Assign x = 5
+= Add and assign x += 3 // x = x + 3
-= Subtract and assign x -= 3
*= Multiply and assign x *= 3
/= Divide and assign x /= 3
%= Modulus and assign x %= 3

🔹 3. Comparison (Relational) Operators

Used to compare two values.

Operator Meaning Example
== Equal to x == y
!= Not equal x != y
> Greater than x > y
< Less than x < y
>= Greater or equal x >= y
<= Less or equal x <= y

Example:

int x = 10, y = 20;
System.out.println(x > y); // false
System.out.println(x < y); // true

🔹 4. Logical Operators

Used to combine multiple conditions.

Operator Meaning Example
&& Logical AND (x > 5 && y < 10)
! Logical NOT !(x > 5)

Example:

int x = 5, y = 10;
System.out.println(x < 10 && y > 5); // true
System.out.println(x < 10 || y < 5); // true
System.out.println(!(x < 10)); // false

🔹 5. Unary Operators

Used with a single operand.

Operator Meaning
+ Positive
- Negative
++ Increment
-- Decrement
! Logical NOT

Example:

int x = 5;
System.out.println(++x); // 6
System.out.println(x--); // 6 (then becomes 5)

🔹 6. Bitwise Operators

Used to perform operations on bits.

Operator Meaning
& Bitwise AND
^ Bitwise XOR
~ Bitwise NOT
<< Left shift
>> Right shift

🔹 7. Ternary (Conditional) Operator

Short form of if-else.

String result = (age >= 18) ? "Adult" : "Minor";

⭐ Example Program Using Operators:

public class Main {
public static void main(String[] args) {
int a = 10, b = 20;
int max = (a > b) ? a : b;
System.out.println("Greater number is: " + max);
}
}

CodeCapsule

Sanjit Sinha — Web Developer | PHP • Laravel • CodeIgniter • MySQL • Bootstrap Founder, CodeCapsule — Student projects & practical coding guides. Email: info@codecapsule.in • Website: CodeCapsule.in

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *