Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

Dp4 Java Operators

The document provides an overview of operators in Java, categorizing them into types such as arithmetic, assignment, logical, relational, unary, bitwise, ternary, and shift operators. Each type is explained with examples of usage and output. Additionally, it includes a quiz section to test knowledge on operator functions.

Uploaded by

hr8000947037
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Dp4 Java Operators

The document provides an overview of operators in Java, categorizing them into types such as arithmetic, assignment, logical, relational, unary, bitwise, ternary, and shift operators. Each type is explained with examples of usage and output. Additionally, it includes a quiz section to test knowledge on operator functions.

Uploaded by

hr8000947037
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 29

P U R

O DH
I J
NST

CSA TRADE TRADE - CSA


INCHARGE HEMRAJ MEENA
MOHAMMAD INSAF R . No.-
OPERATORS IN
JAVA
OPERATOR
Operators are the symbols that are used to
perform various operations on variables and
manipulate the operand values.
Operators in Java are used to perform various
types of operations.
TYPES OF OPERATORS
1. Arithmetic Operators
2. Assignment Operators
3. Logical Operators
4. Relational Operators
5. Unary Operators
6. Bitwise Operators
7. Ternary Operators
8. Shift Operators
1. Arithmetic Operators:
Arithmetic Operators are used to performing mathematical operations like addition,
subtraction, etc. Assume that A = 20 and B = 10 for the below table.
Operator Description Example
+ Addition Adds values on either A+B=30
- Subtraction side of the operator Subtracts the right-hand A-B=10
operator with left-hand

* Multiplication Multiplies values on either side of the A*B=200


operator

/ Division Divides left hand operand with right hand A/B=2


operator

% Modulus Divides left hand operand by right hand A%B=0


operand and returns remainder
class Arithmetic { Example
public static void main (String s[]){
int num1 = 20, num2 = 10;
int sum = num1 + num2;
System.out.println("The sum is: " + sum);
int difference = num1 - num2; OUTPUT
System.out.println("The difference is: " + difference); The sum is: 30
int multiple = num1 * num2; The difference is: 10
System.out.println("The multiple is: " + multiple); The multiple is: 200
int quotient = num1 / num2; The quotient is: 2
System.out.println("The quotient is: " + quotient); The remainder is: 0
int remainder = num1 % num2;
System.out.println("The remainder is: " + remainder);
}
}
2. Assignment Operators
Assigns the value of the right operand to the left operand.
 += (Addition Assignment): Adds the right operand to the left operand and
assigns the result to the left operand.
 -= (Subtraction Assignment): Subtracts the right operand from the left operand
and assigns the result to the left operand.
 *= (Multiplication Assignment): Multiplies the left operand by the right operand
and assigns the result to the left operand.
 /= (Division Assignment): Divides the left operand by the right operand and
assigns the result to the left operand.
 %= (Modulus Assignment): Computes the modulus of the left operand and the
right operand and assigns the result to the left operand
class Assignment {
public static void main (String s[]){ Example
int a = 10;
System.out.println("Assigned Value: " + a);
a += 5; // Equivalent to a = a + 5;
System.out.println("Addition Assignment: " + a);
a -= 3; // Equivalent to a = a - 3;
System.out.println("Subtraction Assignment: " + a);
a *= 2; // Equivalent to a = a * 2;
System.out.println("Multiplication Assignment: " + a); OUTPUT
a /= 4; // Equivalent to a = a / 4; Assigned Value: 10
System.out.println("Division Assignment: " + a); Addition Assignment: 15
a %= 5; // Equivalent to a = a % 5; Subtraction Assignment: 12
Multiplication Assignment:
System.out.println("Modulus Assignment: " + a);
24
} Division Assignment: 6
}
3. Logical operators
Logical operators are used to check whether an expression is True or False.
They are used in decision-making.

• && (Logical AND): Returns true if both operands are true.


• || (Logical OR): Returns true if at least one operand is true.
• ! (Logical NOT): Returns the opposite boolean value of the operand.
Example

class Logical {
public static void main (String s[]){
boolean a = true; OUTPUT
boolean b = false;
System.out.println("a && b: " + (a && b)); a && b: false
System.out.println("a || b: " + (a || b)); a || b: true
System.out.println("!a: " + (!a)); !a: false
}
}
4. Relational Operators

• == (Equal to): Checks if two operands are equal.


• != (Not equal to): Checks if two operands are not equal.
• < (Less than): Checks if the left operand is less than the right operand.
• > (Greater than): Checks if the left operand is greater than the right
operand.
• <= (Less than or equal to): Checks if the left operand is less than or equal to
the right operand.
• >= (Greater than or equal to): Checks if the left operand is greater than or
equal to the right operand.
Example

class Relational {
public static void main (String s[]){
int var1 = 5, var2 = 10, var3 = 5;
System.out.println("var1 == var2: " + (var1 == var2)); OUTPUT
System.out.println("var1 == var3: " + (var1 == var3));
System.out.println("var1 != var2: " + (var1 != var2)); var1 == var2: false
System.out.println("var1 != var3: " + (var1 != var3)); var1 == var3: true
System.out.println("var1 > var2: " + (var1 > var2)); var1 != var2: true
System.out.println("var2 > var3: " + (var2 > var3)); var1 != var3: false
} var1 > var2: false
} var2 > var3: true
5. Unary Operators
unary operators perform operations on a single operand.

1.Arithmetic Unary Operators:


•++: Increment the value of the variable by 1.
•--: Decrement the value of the variable by 1.
2.Logical Unary Operator:
•!: Negate the boolean value of the expression.
3.Bitwise Unary Operator:
•~: Bitwise negation of the variable.
4.Unary Plus and Minus:
•+expression: Indicates a positive value.
•-expression: Indicates a negative value.
Example

•Increment: x = 5; ++x; (now x is 6)


•Decrement: y = 8; --y; (now y is 7)
•Unary Plus: a = -3; b = +a; (now b is -3)
•Unary Minus: c = 4; d = -c; (now d is -4)
•Logical NOT: flag = true; result = !flag; (result is false)
•Bitwise NOT: num = 5; result = ~num; (result is -6)
class Unary {
public static void main (String s[]){
Example
int x = 5;
x++; // Post-increment
System.out.println("Increased value: " +
x);
--x; // Pre-decrement OUTPUT
System.out.println("Decreased value: "
+ x); Increased value: 6
x = -x; // Applying unary minus Decreased value: 5
System.out.println("Unary minus value: Unary minus value: -5
" + x); Unary plus value: -5
x = +x;// Applying unary plus
System.out.println("Unary plus value: "
+ x);}}
Example

class Logicalnotunaruy {
public static void main (String
s[]){
boolean condition = true; OUTPUT
System.out.println("condition is: condition is: true
" + condition); Now condition is: false
System.out.println("Now condition
is: " + !condition);}}
6.Bitwise
Operators
Bitwise operators are used on (binary) numbers:
Compares corresponding bits of two integers. If both bits are 1,
Bitwise AND (&) the result bit is 1; otherwise, it’s 0.
Sets each bit to 1 if both bits are 1
Compares corresponding bits and returns 1 if either operand bit
Bitwise OR (|) is 1.
Sets each bit to 1 if one of two bits is 1
Compares corresponding bits and returns 1 if exactly one
Bitwise XOR (^) operand bit is 1.
Sets each bit to 1 if only one of two bits is 1
Inverts individual bits.
Bitwise NOT (~) Inverts all the bits
Bitwise AND Bitwise XOR
(&) (^)
Decimal Binary Decimal Binary

17= 1 0 0 0 1 17= 1 0 0 0 1

3= 0 0 0 1 1 3= 0 0 0 1 1

1= 1 18= 1 0 0 1 0

Bitwise NOT
Bitwise OR (|)
(~)
Decimal Binary Decimal Binary
17= 0 0 0 0 0 0 0 0 0 0 0 1 0 0
17= 1 0 0 0 1 0 1
0 becomes 1 and 1 1 1 1 1 1 1 1 1 1 1 0 1 1
3= 0 0 0 1 1
1 becomes 0 1 0
19= 1 0 0 1 1 -18= 1 1 1 1 1 1 1 1 1 1 1 0 1 1
1 0
Example
class Bitwise {
public static void main(String[] args) {
int number1 = 12, number2 = 25;
int result = number1 | number2;
int result1 = number1 & number2; OUTPUT
int result2 = number1 ^ number2;
int result3 = ~number1; 29
System.out.println(result); 8
System.out.println(result1); 21
System.out.println(result2); -13
System.out.println(result3);
}
}
7. Ternary Operators
Ternary operators, also known as conditional operators, are used in
programming to evaluate a condition and return one of two values
depending on whether the condition is true or false. They are called
“ternary” because they take three operands: the condition, the result for
true, and the result for false.

If else Syntax: Ternary operator syntax:


if (condition) {
result = condition ? expression1 :
variable = value_if_true; expression2;
} else {
variable = value_if_false;
}
Example
public class Ternary {
public static void main(String[] args) {
int number = 13;
String result;
// Using ternary operator to check if number is even or odd
result = (number % 2 == 0) ? "Even" : "Odd";
System.out.println("The number " + number + " is " + result + ".");
}
}

OUTPU
T
The number 13 is
Odd.
8. Shift Operators

There are three main types of shift operators in java.

1. Signed Left Shift Operator (<<)


2. Signed Right Shift Operator (>>)
3. Unsigned Right Shift Operator (>>>)
1.Signed Left Shift Operator (<<)
•The signed left shift operator << shifts the bits of the left operand to
the left by the number of positions specified by the right operand.
•For each shift left, the high-order bits outside the range of the data
type are discarded, and zeros are brought in on the right.
Example:
int a = 1;
// Binary: 0001
int result = a << 2; OUTPUT
// Binary: 0100, Decimal: 4
2. Signed Right Shift Operator (>>)
•The signed right shift operator >> shifts the bits of the left operand to the
right by the number of positions specified by the right operand.
•The leftmost bits filled after the shift depend on the sign of the initial
number. It preserves the sign bit (the leftmost bit), so if the number is
negative, the result of the shift will also be negative.
•Example:
Java
int b = 8; OUTP
// Binary: 1000 UT
int result = b >> 2; Decimal: 2
// Binary: 0010,
1. Which operator is used for
equality comparison in Java?
a) +
b) =
c) ==
d) &&

Ans:
c
2. Which operator is used for performing
logical AND in Java?
a) &&
b) ||
c) !
d) &

Ans: a
3 Which operator is used to increment a
variable by one in Java?
a) *=
b) --
c) +=
d) ++

Ans: d
4 Which operator is used to invert the
value of a boolean variable?
•A) ~
•B) !
•C) -
•D) ^

Ans: b

You might also like