UNIT IV – OPERATORS IN JAVA
1. Introduction to Operators
An operator is a symbol used to perform mathematical or logical operations on values or variables.
Example: a + b
Here a and b are operands and + is the operator.
2. Operator Structure
Operand Operator Operand
A + b
Diagram Representation:
Operand Operator Operand
a + b
3. Expressions in Java
An expression is a combination of variables, constants and operators.
Example: a + b * c
Expression Diagram:
Expression
|
----------------------
| | |
Operand Operator Operand
4. Types of Operators
Operators are classified into Arithmetic, Relational and Logical operators.
Operators
|
---------------------------------------
| | |
Arithmetic Relational Logical
5. Arithmetic Operators
Operator Meaning Example
+ Addition a+b
- Subtraction a-b
* Multiplication a*b
/ Division a/b
% Remainder a%b
6. Unary Operators
Unary operators operate on only one operand.
Unary Operators
|
-----------------------
| | |
+ - ++ --
Unary plus keeps value unchanged. Unary minus changes the sign.
7. Increment Operator
Increment operator increases value by 1.
Example: a++
Example Program:
class IncrementExample
{
public static void main()
{
int a = 5;
a++;
[Link](a);
}
}
8. Decrement Operator
Decrement operator decreases value by 1.
Example: a--
9. Prefix and Postfix Operators
Prefix operator: ++a (value increases before use)
Postfix operator: a++ (value increases after use)
Prefix Diagram
++a
Postfix Diagram
a++
10. Binary Operators
Binary operators work on two operands.
Example: a + b
11. Shorthand Operators
Expression Shorthand
a=a+5 a += 5
a=a-3 a -= 3
a=a*2 a *= 2
a=a/2 a /= 2
12. Ternary Operator
The ternary operator works with three operands.
Syntax: condition ? expression1 : expression2
Condition
/ True False
Exp1 Exp2
13. Relational Operators
Operator Meaning
< Less than
> Greater than
<= Less than or equal
>= Greater than or equal
== Equal
!= Not equal
14. Logical Operators
Logical Operators
|
-------------------------
| | |
AND OR NOT
&& || !
15. Logical AND Truth Table
Condition1 Condition2 Result
True True True
True False False
False True False
False False False
16. Operator Precedence
Logical operator precedence: NOT → AND → OR
Precedence Diagram
NOT
↓
AND
↓
OR
17. Chapter Summary
Operators perform operations on operands.
Unary operators work on one operand.
Binary operators work on two operands.
Ternary operator works on three operands.
Relational operators compare values.
Logical operators combine conditions.