0% found this document useful (0 votes)
24 views29 pages

Lec 3

Uploaded by

bhavaya65
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)
24 views29 pages

Lec 3

Uploaded by

bhavaya65
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/ 29

Java Operators

• Java operators are symbols or keywords that perform


operations on variables and values. They are broadly
categorized into the following types:

• 1 - Arithmetic Operators
• 2 - Relational (Comparison) Operators
• 3 - Logical Operators
• 4 - Assignment Operators
• 5 - Unary Operators
• 6 - Bitwise Operators
• 7 - Ternary Operator
• 8 - Instanceof Operator
• 1. Arithmetic Operators
• Used to perform basic mathematical operations.
• Example-
public class ArithmeticExample {
public static void main(String[] args) {
int a = 10, b = 5;
System.out.println("Addition: " + (a + b)); // 15
System.out.println("Subtraction: " + (a - b)); // 5
System.out.println("Multiplication: " + (a * b)); // 50
System.out.println("Division: " + (a / b)); // 2
System.out.println("Modulus: " + (a % b)); // 0
}
}
• 2. Relational (Comparison) Operators
• Used to compare two values.
• Example-
public class RelationalExample {
public static void main(String[] args) {
int a = 10, b = 5;
System.out.println("Equal to: " + (a == b)); // false
System.out.println("Not equal to: " + (a != b)); // true
System.out.println("Greater than: " + (a > b)); // true
System.out.println("Less than: " + (a < b)); // false
System.out.println("Greater than or equal to: " + (a >= b)); // true
System.out.println("Less than or equal to: " + (a <= b)); // false
}
}
• 3. Logical Operators
• Used for logical operations.
• Example-
public class LogicalExample {
public static void main(String[] args) {
int a = 10, b = 5, c = 15;

// Logical AND: true if both conditions are true


System.out.println("Logical AND: " + ((a > b) && (c > a))); // true

// Logical OR: true if at least one condition is true


System.out.println("Logical OR: " + ((a < b) || (c > a))); // true

// Logical NOT: reverses the truth value


System.out.println("Logical NOT: " + !(a > b)); // false
}
}
• 4. Assignment Operators
• Used to assign values to variables.
• Example –
public class AssignmentExample {
public static void main(String[] args) {
int a = 10;
a += 5; // a = a + 5
System.out.println("a after +=: " + a); // 15
a -= 2; // a = a - 2
System.out.println("a after -=: " + a); // 13
a *= 2; // a = a * 2
System.out.println("a after *=: " + a); // 26
a /= 2; // a = a / 2
System.out.println("a after /=: " + a); // 13
}
}
• 5. Unary Operators
• Operate on a single operand.
• Example –
public class UnaryExample {
public static void main(String[] args) {
int a = 10;
System.out.println("Post-increment: " + a++); // 10 (prints, then increments)
System.out.println("After Post-increment: " + a); // 11
System.out.println("Pre-increment: " + ++a); // 12 (increments, then prints)
System.out.println("Unary minus: " + (-a)); // -12
}
}
• 6. Bitwise Operators
• Bitwise operators are used to performing the manipulation of
individual bits of a number.
• Bitwise OR Operator
• The bitwise OR | operator returns 1 if at least one of the
operands is 1. Otherwise, it returns 0.
• Let's look at the bitwise OR operation of two integers 12 and
25.
• Bitwise AND Operator
• The bitwise AND & operator returns 1 if and only if both the
operands are 1. Otherwise, it returns 0.
• Bitwise XOR Operator
• The bitwise XOR ^ operator returns 1 if and only if one of the
operands is 1. However, if both the operands are 0 or if both
are 1, then the result is 0.
• Bitwise Complement Operator
• The bitwise complement operator is a unary operator (works with only
one operand). It is denoted by ~.
• It changes binary digits 1 to 0 and 0 to 1.
• It is important to note that the bitwise complement of any integer N is
equal to - (N + 1). For example,
• Consider an integer 35. As per the rule, the bitwise complement
of 35 should be -(35 + 1) = -36. Now let's see if we get the correct answer
or not.

• In the above example, we get that the bitwise complement


of 00100011 (35) is 11011100. Here, if we convert the result into decimal
we get 220.
• However, it is important to note that we cannot directly
convert the result into decimal and get the desired output.
This is because the binary result 11011100 is also equivalent
to -36. Let's understand it -
• The result 11011100 is a signed 8-bit binary number. The
leftmost bit (1) indicates that it's a negative number (in the
case of an 8-bit signed integer, the leftmost bit determines
whether the number is positive or negative).
• To find out what negative number 11011100 represents, you
need to convert it to its positive equivalent by following the
2's complement process:
• Invert the bits of 11011100: 00100011
• Add 1: 00100011 + 1 = 00100100 (which is 36 in decimal).
• Since the original leftmost bit indicated that it's negative, the
number is -36.
• Example –
class Main {
public static void main(String[] args) {

int number = 35, result;

// bitwise complement of 35
result = ~number;
System.out.println(result); // prints -36
}
}
• Left Shift Operator
• The left shift operator shifts all bits towards the left by a certain
number of specified bits. It is denoted by <<.

• As we can see from the image above, we have a 4-digit number.


When we perform a 1 bit left shift operation on it, each
individual bit is shifted to the left by 1 bit.
• As a result, the left-most bit (most-significant) is discarded and
the right-most position(least-significant) remains vacant. This
vacancy is filled with 0s.
• Example –
class Main {
public static void main(String[] args) {

int number = 2;

// 2 bit left shift operation


int result = number << 2;
System.out.println(result); // prints 8
}
}
• Signed Right Shift Operator
• The signed right shift operator shifts all bits towards the right
by a certain number of specified bits. It is denoted by >>.
• When we shift any number to the right, the least significant
bits (rightmost) are discarded and the most significant
position (leftmost) is filled with the sign bit. For example,

• Here, we are performing the right shift of 8 (i.e. sign is


positive). Hence, there no sign bit. So the leftmost bits are
filled with 0 (represents positive sign).
• Here, we have used the signed bit 1 to fill the leftmost bits.
• The result will be -2 (by 2’s complement of 1110)
• Example –
class Main {
public static void main(String[] args) {

int number1 = 8;
int number2 = -8;

// 2 bit signed right shift


System.out.println(number1 >> 2); // prints 2
System.out.println(number2 >> 2); // prints -2
}
}
• Unsigned Right Shift Operator
• Java also provides an unsigned right shift. It is denoted by >>>.
• Here, the vacant leftmost position is filled with 0 instead of
the sign bit. For example,
• Example –
class Main {
public static void main(String[] args) {

int number1 = 8;
int number2 = -8;

// 2 bit signed right shift


System.out.println(number1 >>> 2); // prints 2
System.out.println(number2 >>> 2); // prints 1073741822
}
}
• 7. Ternary Operator
• Shorthand for an if-else statement.

• Example –
public class TernaryExample {
public static void main(String[] args) {
int a = 10, b = 20;
int max = (a > b) ? a : b;
System.out.println("Maximum: " + max); // 20
}
}
• 8 - Instanceof Operator
• Used to check if an object is an instance of a particular class.
• Example –
String str = "Hello";
System.out.println(str instanceof String); // true

You might also like