About Lesson
Unary Operator
Unary operators require only one operand. They are used to perform various operations:
- incrementing/decrementing a value by one
- negating an expression
- inverting value of the boolean
Arithmetic Operator
They are used in mathematical expressions. Operands of arithmetic operators must be of numeric type and char (char in Java is a subset of int).
+ Unary Plus
: it returns value of its single operand.- Unary Minus
: It negates its single operand.+ - * /
: It would work like it works in general maths.% Modulus
: it returns remainder of division operation. Can be applied to floating types and integer types.+= -= *= %= /=
: they are compound assignment operator. egi = i + 2
is same asi+= 2
i = i / 2
is same asi /= 2
++ Increment -- Decrement
: It increases/decreases operand by one respectively.- Postfix:
i++ i--
When operators used in large expression (egint i = 2; i = i++ + ++i
), previous value is obtained for used in expression and then operand is modified. - Prefix:
++i --i
When operators used in large expression (egint j = 10; j = --j + ++j
), operand is incremented or decremented before the value is obtained for use in the expression.
- Postfix:
-- Decrement
: It decreases operand by one.
public class ArithmeticOperatorsExample {
public static void main(String[] args) {
int a = 10;
int b = 5;
// Addition (+): Adds two operands
int sum = a + b; // 10 + 5 = 15
System.out.println("Addition: " + sum);
// Subtraction (-): Subtracts the second operand from the first
int difference = a - b; // 10 - 5 = 5
System.out.println("Subtraction: " + difference);
// Multiplication (*): Multiplies two operands
int product = a * b; // 10 * 5 = 50
System.out.println("Multiplication: " + product);
// Division (/): Divides the first operand by the second (integer division)
int quotient = a / b; // 10 / 5 = 2
System.out.println("Division: " + quotient);
// Modulus (%): Returns the remainder of the division of the first operand by the second
int remainder = a % b; // 10 % 5 = 0 (no remainder)
System.out.println("Modulus: " + remainder);
// Increment (++): Increases the value of the operand by 1
a++; // Equivalent to a = a + 1; (a becomes 11)
System.out.println("Increment: " + a);
// Decrement (--): Decreases the value of the operand by 1
b--; // Equivalent to b = b - 1; (b becomes 4)
System.out.println("Decrement: " + b);
// Compound Assignment Operators: Combine arithmetic operations with assignment
a += 2; // Equivalent to a = a + 2; (a becomes 13)
System.out.println("Compound Assignment (Addition): " + a);
b *= 3; // Equivalent to b = b * 3; (b becomes 12)
System.out.println("Compound Assignment (Multiplication): " + b);
// Postfix Increment: Increases the value of the operand by 1, returns the original value
int postIncrementA = a++; // a = 10, postIncrementA = 10 (a becomes 11)
System.out.println("Postfix Increment: " + postIncrementA + ", a: " + a);
// Prefix Increment: Increases the value of the operand by 1, returns the updated value
int preIncrementB = ++b; // b = 6, preIncrementB = 6
System.out.println("Prefix Increment: " + preIncrementB + ", b: " + b);
// Postfix Decrement: Decreases the value of the operand by 1, returns the original value
int postDecrementA = a--; // a = 11, postDecrementA = 11 (a becomes 10)
System.out.println("Postfix Decrement: " + postDecrementA + ", a: " + a);
// Prefix Decrement: Decreases the value of the operand by 1, returns the updated value
int preDecrementB = --b; // b = 5, preDecrementB = 5
System.out.println("Prefix Decrement: " + preDecrementB + ", b: " + b);
}
}