Java-Operators
Java-Operators
Examples:
5+3=8 (addition)
10 − 4 = 6 (subtraction)
3 ∗ 4 = 12 (multiplication)
15 / 3 = 5 (division)
17 % 5 = 2 (modulus, remainder)
int a = 10;
int b = 3;
System.out.println("Sum: " + (a + b));
System.out.println("Difference: " + (a -
b));
System.out.println("Product: " + (a * b));
System.out.println("Quotient: " + (a / b));
System.out.println("Remainder: " + (a %
b));
Relational Operators
• Compare two values
• Return a boolean result (true or
false)
• Include: ==, !=, <, >, <=,
>=
Examples:
Logical Operators
• Used to combine
multiple conditions
• Include: && (AND), ||
(OR), ! (NOT)
The AND Operator (&&
• Symbol: &&
• Returns true if both operands are true
• Truth table:
true && true = true
true && false = false
false && true = false
false && false = false
The OR Operator (||)
• • Symbol: ||
• Returns true if at least one operand is true
• Truth table:
true || true = true
true || false = true
false || true = true
false || false = false
The NOT Operator (!)
• Symbol: !
• Reverses the boolean value of its
operand
• Example:
!true = false
!false = true
• Used to negate a boolean expression or
variable
import java.io.*;
class GFG {
public static void main(String[] args)
{ Output:
boolean cond = true; Cond is: true
int a = 10, b = 1; Var1 = 10
Var2 = 1
System.out.println("Cond is: " + cond);
System.out.println("Var1 = " + a); Now cond is:
System.out.println("Var2 = " + b); false
!(a < b) = true
System.out.println("Now cond is: " + ! !(a > b) = false
cond);
System.out.println("!(a < b) = " + !(a <
b));
System.out.println("!(a > b) = " + !(a >
b));
}
Common Use Case: If
Statements
• • Logical operators are often used in if
statements
• Example:
if (age >= 16 && hasLicense) {
System.out.println("You can
drive"); }
Relational and Logical
Operators Example
int x = 5, y = 10;
boolean result = (x < y) && (y % 2 ==
0);
System.out.println("Is x less than y AND
y even? " + result);
Assignment Operators
• • Assign values to variables
• Basic assignment: =
• Compound assignments: +=, -=,
*=, /=, %=
• Examples:
x=5x=5
y+=3y (equivalent to y=y+3)
z∗=2 (equivalent to z=z∗2)
int num = 5;
num += 3; // num is now 8
num *= 2; // num is now 16
num /= 4; // num is now 4
System.out.println("Final value: " +
num);
Unary Operators
• • Operate on a single operand
• Include: +, -, ++, --, !
• Examples:
+5 (positive number)
−3 (negative number)
x++(increment by 1)
y−− (decrement by 1)
Ternary Operator
System.out.println(result);
Ternary operator
System.out.println(result); Result:
Fail
syntax
public class TernaryOperatorExample {
public static void main(String[] args)
{
boolean condition = true;
String result = (condition) ? "True" : "False";
System.out.println(result);
}
}
Increment(++) Operator
It is used to increment the value of an integer.
Post-increment operator: When placed after the variable name,
the value of the operand is incremented but the previous value is retained
temporarily until the execution of this statement, and it gets updated
before the execution of the next statement.
Syntax: Illustration:
num++ num = 5
num++ = 6
Pre-increment operator: When placed before the variable name,
the operand’s value is incremented instantly.
Syntax: Illustration:
++num num = 5
++num = 6
Decrement(– –) Operator
It is used to decrement the value of an integer. It can be used in two
separate ways
Post-decrement operator
When placed after the variable name, the value of the operand is
decremented but the previous values is retained temporarily until the
execution of thisIllustration:
statement and it gets updated before the execution of the
next statement. num = 5
Syntax: num-- = 5 // Value will be decremented before
num-- execution of next statement.
Pre-decrementIllustration:
operator
When placed before the
num variable
= 5 name, the operand’s value is
decremented instantly.--num = 5 //output is 5, value is decremented before
Syntax: execution of next statement
Increment and Decrement
Operators
• int count = 0;
• System.out.println(count++); // Prints 0, then
increments
• System.out.println(++count); // Increments, then
prints 2
• System.out.println(count--); // Prints 2, then
decrements
• System.out.println(--count); // Decrements, then
prints 0