0% found this document useful (0 votes)
10 views30 pages

Operators

The document provides an overview of operators in Java, explaining expressions, operands, and various types of operators including arithmetic, relational, logical, assignment, unary, ternary, and bitwise operators. It details how these operators perform specific operations on data, allowing for calculations, comparisons, and manipulations essential for programming. Additionally, it highlights the significance of ASCII and the use of operators in enhancing code readability and performance.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views30 pages

Operators

The document provides an overview of operators in Java, explaining expressions, operands, and various types of operators including arithmetic, relational, logical, assignment, unary, ternary, and bitwise operators. It details how these operators perform specific operations on data, allowing for calculations, comparisons, and manipulations essential for programming. Additionally, it highlights the significance of ASCII and the use of operators in enhancing code readability and performance.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 30

Operators In Java

 Expression : the combination of operands and operators which


produces results is known as expression.
Or
The combination of symbols and data’s which produces the result is
known as expression.
Ex: 2 + 20= 22 , Ex2: 2 * 4 = 8

Result = operand1 (operator) operand2

 Operand: It is a data which is used to perform a task.


 Operator: It is symbol which will perform a task based on given
operands.
Operators
 Operators in Java are special symbols that perform specific operations on one,
two, or three operands and then return a result.
 Operators are essential for performing different types of operations on
variables and values within the program.
 Why Do We Need Java Operators
• Operators allow the execution of arithmetic calculations essential for any
mathematical operations in your program.

• They enable decision-making in the code through comparison and logical


operators, directing the execution flow based on conditions.

• They are used to manipulate data at both bit and byte levels, allowing for
efficient data processing.

• Operators like the assignment and unary operators simplify code by reducing
complexity and increasing readability.

• Operators, especially bitwise and shift operators, can perform operations faster
than their functional equivalents, enhancing performance.
ASCII
 ASCII (American Standard Code for Information Interchange) is a
character encoding standard used in computers and electronic devices
to represent text.
 It assigns a unique numerical value to each character, including letters,
digits, punctuation marks, and control characters.
ASCII Decimal
Character Range Characters
Range
Uppercase Letters 'A' → 'Z' 65 → 90

Lowercase Letters 'a' → 'z' 97 → 122

Digits '0' → '9' 48 → 57

In Java, char ch = 'A'; stores the Unicode value 65 internally, which matches the
ASCII value for basic characters .

• An ASCII value (for standard English characters)


• A Unicode value (for all characters including symbols and other
languages)
Types of Java Operators

• Arithmetic Operators

• Relational (Comparison) Operators

• Logical Operators

• Assignment Operators

• Unary Operators

• Ternary Operator

• Shift Operators

• Bitwise Operators
1. Arithmetic Operators
 Arithmetic operators are used to perform basic mathematical
calculations. Java supports addition (+), subtraction (-), multiplication
(*), division (/), and modulus (%).
 It is a binary operator (Which accepts two data to perform a task.)
• Addition (+)

• Subtraction (-)

• Multiplication (*)

• Division (/)

• Modulus (%): Returns the remainder of a division operation


Ex: 10 + 50 , 50*4 , 60/30
Different Cases of Addition (Using + Operator):
 Case I: Number + Number
When two numbers are added, the result is a number.
Example: 10 + 30 = 40

 Case II: Number + Character


When a number is added to a character, the character is converted to its ASCII value before
addition.
Example: 100 + 'A' = 165 (since ASCII value of 'A' is 65)

 Case III: String + Number


When a string is added to a number, the number is converted to a string, and the result is a
string (concatenation).
Example: "Age: " + 25 = "Age: 25“

 Case IV: String + Character


When a string is added to a character, the result is also a string (concatenation).
Example: "Grade: " + 'A' = "Grade: A"
If we apply any Arithmetic operation b/w 2 variables a & b ,
the result type is always max(int , type of a , type of b).

 Example :
 byte + byte = int
 byte + short = int
 short + short = int
 short + long = long
 double + float = double
 int + double = double
 char + char = int
 char + int = int
 char + double = double
 long + float = float
 int + long = long
Left Right
Result
Operator Name Operand Operand Notes
Type
(Part 1) (Part 2)

int, double, Can add numbers or


+ Addition Addend Addend
String, char concatenate strings

int, double, char is converted to


- Subtraction Minuend Subtrahend
char ASCII

int, double,
* Multiplication Multiplicand Multiplier Result is numeric
char
int, double, Division by 0 causes
/ Division Dividend Divisor
char error
Modulus int, double,
% Dividend Divisor Gives remainder
(Remainder) char
EX:
int a = 10; int b = 5;
// Addition
int sum = a + b;
System.out.println("Sum: " + sum);

// Subtraction
int difference = a - b;
System.out.println("Difference: " + difference);

// Multiplication
int product = a * b;
System.out.println("Product: " + product);

// Division
int quotient = a / b;
System.out.println("Quotient: " + quotient);

// Modulus (Remainder)
int remainder = a % b;
System.out.println("Remainder: " + remainder);
Relational (Comparison) Operators

 Relational operators are used to compare values and determine their


relationship. They include equality (==), inequality (!=), greater than (>),
less than (<), greater than or equal to (>=), and less than or equal to (<=)
• Equal to (==)  It will give result as true if both operands exactly same
otherwise false

• Not equal to (!=)  Opposite to equal to operator. { 5 != 10 == true}

• Greater than (>) it will give resilt as true if the op1 is greater than op2
otherwise result is false.

• Less than (<) it will give resilt as true if the op1 is less than op2
otherwise result is false.

• Greater than or equal to (>=) it will give resilt as true if the op1 is
greater than equals to op2 otherwise result is false. { Ex: 5>= 3 = true}

• Less than or equal to (<=) it will give resilt as true if the op1 is less
than equals to op2 otherwise result is false. { ex: 5<= 6 = true }
Example for comparison operator
int a = 5; int b = 10;

// Equal to (==)
System.out.println("Is a equal to b? " + (a == b));

// Not equal to (!=)


System.out.println("Is a not equal to b? " + (a != b));

// Greater than (>)


System.out.println("Is a greater than b? " + (a > b));

// Less than (<)


System.out.println("Is a less than b? " + (a < b));

// Greater than or equal to (>=)


System.out.println("Is a greater than or equal to b? " + (a >= b));

// Less than or equal to (<=)


Logical Operators

 Logical operators are used to perform logical operations on boolean


values. They include logical AND (&&), logical OR (||), and logical NOT (!).

• AND (&&)  It produces output as true if both given operands are


true otherwise false.
Op1 Op2 Result
true true true

true false false

false true false

false false false

• Syntax: boolean varName =op1 && op2 //return only Boolean value

ex: Boolean b= (20>5) && (3>1) // true


Logical OR (||) operator:

 It will give result as true if any one of the operand is true otherwise
result will be false.
 It works on the principle of a logical operator ‘OR’.

Op1 Op2 Result


true true true
true false true
false true true
false false false

syntax: op1 || op2 = true/false


EX: boolean k = (3>55) || (9<99) // true
Logical NOT (!) operator:

 It produces output as true if the given operand is false otherwise result


will be false.

Syntax: ! (operand)
 It is a unary operator takes only one data
 The ! (NOT) operator in Java takes a boolean input and returns a
boolean output by inverting its value.

Operand Result
true false

false true

 Ex: boolean b= !(true) // false


ex: boolean b2 = !(20> 5) // true
Assignment Operators
 Assignment operators are used to assign values to variables. The most common
assignment operator is the equal sign (=).
• Simple Assignment (=)  Used to assign a value to a variable.
• Syntax: variable = value;

• Compound Assignment (e.g., +=, -=, *=, /=, %=): Performs the operation and assigns
the result to the variable.
• These are shortcuts for common assignments.
Operator Meaning Example Same as
+= Add and assign a += 5 a=a+5

-= Subtract and assign a -= 2 a=a-2

*= Multiply and assign a *= 3 a=a*3

/= Divide and assign a /= 2 a=a/2

%= Modulus and assign a %= 2 a=a%2


// Assignment operator (=)
int a = 10;
System.out.println("a = " + a);

// Addition assignment operator (+=)


a += 5; // Equivalent to: a = a + 5;
System.out.println("After a += 5, a = " + a);

// Subtraction assignment operator (-=)


a -= 3; // Equivalent to: a = a - 3;
System.out.println("After a -= 3, a = " + a);

// Multiplication assignment operator (*=)


a *= 2; // Equivalent to: a = a * 2;
System.out.println("After a *= 2, a = " + a);

// Division assignment operator (/=)


a /= 4; // Equivalent to: a = a / 4;
System.out.println("After a /= 4, a = " + a);
// Modulus assignment operator (%=)
a %= 3; // Equivalent to: a = a % 3;
System.out.println("After a %= 3, a = " + a);

// Left shift assignment operator (<<=)


b <<= 2; // Equivalent to: b = b << 2;
System.out.println("After b <<= 2, b = " + b);

// Right shift assignment operator (>>=)


b >>= 1; // Equivalent to: b = b >> 1;
System.out.println("After b >>= 1, b = " + b);

// Unsigned right shift assignment operator (>>>=)


b >>>= 1; // Equivalent to: b = b >>> 1;
System.out.println("After b >>>= 1, b = " + b);
Unary Operators
 Unary operators operate on a single operand.
 They perform operations like negation, increment, decrement, etc.
 Common unary operators in Java:
 + (Unary plus)
 - (Unary minus),
 ++ (Increment),
 -- (Decrement)
Unary Plus (+):
 Represents the identity of the operand.
 It does not change the value, just returns the value as-is.
 Usually optional and rarely used explicitly.
Ex: int x = 5;
int y = +x; // y is 5, same as x

Unary Minus (-):


 Negates the value of the operand.
 Converts positive to negative and vice versa.
Ex: int x = 5;
int y = -x; // y is -5
Increment Operator (++)
 Adds 1 to the operand.
 Can be used in two ways:
 Prefix (++x): Increment first, then use the value.
 Postfix (x++): Use the value first, then increment.
 Prefix: increments the variable before it’s used in an expression.
 Postfix: increments the variable after it’s used.
 Mostly used in loops and counters.
Form Syntax
Prefix ++variable;
Postfix variable++;
Ex: int x = 5;
int a = ++x; // x = 6, a = 6
int b = x++; // b = 6, x = 7
Decrement Operator (--)
 Subtracts 1 from the operand.
 Can be used in two ways:
 Prefix (--x): Decrement first, then use the value.
 Postfix (x--): Use the value first, then decrement.
 Prefix: decrements the variable before it’s used in an expression.
 Postfix: decrements the variable after it’s used.
 Commonly used in countdown loops.
Form Syntax
Prefix --variable;
Postfix variable--;

Ex: int x = 5;
int a = --x; // x = 4, a = 4
int b = x--; // b = 4, x = 3
Ternary Operator

 The ternary operator is a shortcut for if-else statements.


 It is called “ternary” because it works with three operands:
 A condition (boolean expression)
 A value returned if the condition is true
 A value returned if the condition is false
 Syntax: (condition) ? expression1 : expression2;
• If condition is true, it returns expression1
• If condition is false, it returns expression2
Ex: int a = 10, b = 20;
int max = (a > b) ? a : b;
System.out.println("Max = " + max);
 Compact: Ternary operator allows writing simple if-else logic in one line.
 Improves readability for simple decisions.
 Should not be overused in complex logic—prefer if-else for clarity.
 Ternary operator can be nested:
VarName result = (a > b) ? a : (b > c) ? b : c;
Ex: int a = 20, b = 35, c = 30;
int max = (a > b) ?
((a > c) ? a : c) :
((b > c) ? b : c);
System.out.println("Maximum: "
+ max);
Bitwise Operators
• Bitwise operators are used to perform operations at the bit level,
manipulating individual bits of numbers.

• These operators are particularly useful for low-level programming,


optimizing performance, and working with flags.

• AND (&)  Sets a bit to 1 if both corresponding bits are 1

• OR (|)  Sets a bit to 1 if at least one of the corresponding bits is 1.

• XOR (^)  Sets a bit to 1 if at least one of the corresponding bits is 1.

• Complement (~) (tilde symbol)  Inverts all bits (1s become 0s, 0s
become 1s).
Real world use cases.
 1. Access Control & Permissions Management
Many systems use bit masking to manage user permissions.
Example: File permissions in Unix/Linux (rwx - read, write, execute) can be
represented using bitwise operations.
 2. Optimizing Performance in Computation
Bitwise operations are faster than arithmetic operations (x / 2 is slower than x
>> 1 for division).
Used in graphics rendering, signal processing, and cryptography for quick
calculations.
 3. Data Compression & Encryption
Helps encode and compress data efficiently.
Used in encryption algorithms (like XOR in cryptography).
EX:
int a = 5; // binary: 0101
int b = 3; // binary: 0011

int andOp = a & b; //and operator


int orOp = a | b; //or operator
int xor = a ^ b; //xor operator
int complimentOp = ~a; //bitwise compliment

 S.o.p(andOp);
 S.o.p(orOp);
 S.o.p(xor);
Shift operator:
 Shift operators in Java are used to manipulate individual bits of a
number.
 These operators shift the bits left or right, effectively multiplying or
dividing the number by powers of two
 Left Shift (<<):
 Shifts bits to the left, filling vacant positions with 0.
 Equivalent to multiplying the number by 2^n (where n is the number of
positions shifted).
 Right Shift (>>):
 Shifts bits to the right, preserving the sign bit for negative numbers.
 Equivalent to dividing the number by 2^n.
 Unsigned Right Shift (>>>):
 Similar to right shift (>>) but fills empty positions with 0, ignoring the sign
bit.
 Used for unsigned data manipulation.
Ex:
 LeftShift:
int a = 5; // Binary: 0000 0101
int result = a << 2; // Left shift by 2 positions
System.out.println(result); // Output: 20 (Binary: 0001 0100)
 Right Shift:
int b = 20; // Binary: 0001 0100
int result = b >> 2; // Right shift by 2 positions
System.out.println(result); // Output: 5 (Binary: 0000 0101)
 Unsigned Right Shift:
int c = -10; // Binary (32-bit representation): 1111...1010
int result = c >>> 2; // Unsigned right shift by 2 positions
System.out.println(result); // Output: A large positive number due to sign bit
removal

You might also like