Operators and Expressions
Operators: Symbols used to perform operations on variables and values.
Expressions: A combination of variables, operators, and values that produce a result.
Example: int result = a + b * c;
Arithmetic Operators
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus
Example: int sum = 5 + 3; // Output: 8
Relational Operators
== Equal to
!= Not equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
Example: if (a > b)
Logical Operators
&& Logical AND
|| Logical OR
! Logical NOT
Example: if (a > b && c > d)
Bitwise Operators
& AND
| OR
^ XOR
~ Complement
<< Left Shift
>> Right Shift
>>> Unsigned Right Shift
Ternary Operator
Syntax: condition ? value_if_true : value_if_false;
Example: int min = (a < b) ? a : b;
Why Arithmetic in Java?
Even with a calculator, arithmetic operations in Java are needed:
- Automate calculations
- Perform dynamic computations in code
- Useful in gaming, finance, simulations, etc.
Data Types That Can't Use
Arithmetic
Cannot perform arithmetic on:
- boolean (true/false)
- Objects (non-numeric unless overridden)
- Strings (except + for concatenation)
Logical Operator Truth Table
A | B | A && B | A || B | !A
T | T | T | T | F
T | F | F | T | F
F | T | F | T | T
F | F | F | F | T
Shift Operators in Java
<< Left Shift (multiply by 2ⁿ)
>> Right Shift (divide by 2ⁿ)
>>> Unsigned Right Shift (fills with 0)
Example:
int a = 5; a << 1 = 10

Java_Operators_and_Shift_Operations (1).pptx

  • 1.
    Operators and Expressions Operators:Symbols used to perform operations on variables and values. Expressions: A combination of variables, operators, and values that produce a result. Example: int result = a + b * c;
  • 2.
    Arithmetic Operators + Addition -Subtraction * Multiplication / Division % Modulus Example: int sum = 5 + 3; // Output: 8
  • 3.
    Relational Operators == Equalto != Not equal to > Greater than < Less than >= Greater than or equal to <= Less than or equal to Example: if (a > b)
  • 4.
    Logical Operators && LogicalAND || Logical OR ! Logical NOT Example: if (a > b && c > d)
  • 5.
    Bitwise Operators & AND |OR ^ XOR ~ Complement << Left Shift >> Right Shift >>> Unsigned Right Shift
  • 6.
    Ternary Operator Syntax: condition? value_if_true : value_if_false; Example: int min = (a < b) ? a : b;
  • 7.
    Why Arithmetic inJava? Even with a calculator, arithmetic operations in Java are needed: - Automate calculations - Perform dynamic computations in code - Useful in gaming, finance, simulations, etc.
  • 8.
    Data Types ThatCan't Use Arithmetic Cannot perform arithmetic on: - boolean (true/false) - Objects (non-numeric unless overridden) - Strings (except + for concatenation)
  • 9.
    Logical Operator TruthTable A | B | A && B | A || B | !A T | T | T | T | F T | F | F | T | F F | T | F | T | T F | F | F | F | T
  • 10.
    Shift Operators inJava << Left Shift (multiply by 2ⁿ) >> Right Shift (divide by 2ⁿ) >>> Unsigned Right Shift (fills with 0) Example: int a = 5; a << 1 = 10