0% found this document useful (0 votes)
8 views

OOP CS3391 UNIT-1[Lecture note-13]

The document provides an overview of Java operators, categorizing them into unary, binary, and ternary operators, along with examples of each type. It explains various operator categories including assignment, arithmetic, relational, logical, bitwise, compound assignment, conditional, and the instanceof operator. Each section includes syntax and example code to illustrate the functionality of the operators in Java.

Uploaded by

RajeeshKumar
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)
8 views

OOP CS3391 UNIT-1[Lecture note-13]

The document provides an overview of Java operators, categorizing them into unary, binary, and ternary operators, along with examples of each type. It explains various operator categories including assignment, arithmetic, relational, logical, bitwise, compound assignment, conditional, and the instanceof operator. Each section includes syntax and example code to illustrate the functionality of the operators in Java.

Uploaded by

RajeeshKumar
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/ 16

OPERATORS

• Operators are used to manipulate primitive data types.


• Java operators can be classified as unary operators, binary or ternary operators —
meaning taking one, two, or three arguments, respectively.

I. Java Unary Operator


The Java unary operators require only one operand. Unary operators are used to
perform various operations
1. incrementing/decrementing a value by one
2. negating an expression
3. inverting the value of a boolean
1.Java Unary Operator Example: ++ and - -

class OperatorExample
{
public static void main(String args[])
{
int x = 10;
System.out.println(x++); // Post-increment: prints 10, then x becomes 11
System.out.println(++x); // Pre-increment: x becomes 12, then prints 12
System.out.println(x--); // Post-decrement: prints 12, then x becomes 11
System.out.println(--x); // Pre-decrement: x becomes 10, then prints 10
}
} Output:
10
12
12
10
2. Java Unary Operator Example: ~ and !

class OperatorExample
{
public static void main(String args[])
{
int a = 10;
int b = -10;
boolean c = true;
boolean d = false;
// Bitwise complement of a and b
System.out.println(~a); // Output: -11 (equivalent to -(a+1))
System.out.println(~b); // Output: 9 (equivalent to -(b+1))
// Logical NOT of boolean variables Output:
System.out.println(!c); // Output: false
System.out.println(!d); // Output: true -11
} 9
} False
true
II. A binary or ternary operators
A binary or ternary operator appears between its arguments.
Java operators fall into eight different categories:
1. Assignment
2. Arithmetic
3. Relational
4. Logical
5. Bitwise
6. Compound assignment
7. Conditional
8. Instance of
1. Java Assignment Operator
The java assignment operator statement has the following syntax:
<variable> = <expression>
If the value already exists in the variable it is overwritten by the assignment operator (=).
Eg.
int a;
a=10;
a=a+5;
Example program:
class OperatorExample
{
public static void main(String[] args)
{
int a = 10;
int b = 20;
a=a+4 Output:
System.out.println(a); 14
System.out.println(b); 20
}}
2. Java Arithmetic Operators
Java arithmetic operators are used to perform addition, subtraction, multiplication,
and division.
They act as basic mathematical operations.
Assume integer variable A holds 10 and variable B holds 20, then:

Operator Description Example


+ Addition - Adds values on either side of the operator A + B will give 30
- Subtraction - Subtracts right hand operand from left A - B will give - 10
hand operand
* Multiplication - Multiplies values on either side of the A * B will give 200
operator
/ Division - Divides left hand operand by right hand B / A will give 2
operand
% Modulus - Divides left hand operand by right hand B % A will give 0
operand and returns remainder
Example program:
class OperatorExample
{
public static void main(String args[])
{
Int w=4, x=10, y=5, z=3
System.out.println(x * x / y + z - 1 * w / 2); Output:
} 21
}
3. Relational Operators
Relational operators in Java are used to compare 2 or more objects.
Java provides six relational operators:
Assume variable A holds 10 and variable B holds 20, then:
Operator Description Example
== Checks if the values of two operands are equal or not, if yes (A == B) is not
then condition becomes true. true.
!= Checks if the values of two operands are equal or not, if values (A != B) is true.
are not equal then condition becomes true.
> Checks if the value of left operand is greater than the value of (A > B) is not true
right operand, if yes then condition becomes true.
< Checks if the value of left operand is less than the value of (A < B) is true.
right operand, if yes then condition becomes true.
>= Checks if the value of left operand is greater than or equal to (A >= B) is not true
the value of right operand, if yes then condition becomes true..
<= Checks if the value of left operand is less than or equal to the (A <= B) is true.
value of right operand, if yes then condition becomes true.
class RelationalOperatorsDemo
{
public static void main(String[] args)
{
int x = 10, y = 5;
System.out.println("x > y : " + (x > y));
System.out.println("x < y : " + (x < y));
System.out.println("x >= y : " + (x >= y));
System.out.println("x <= y : " + (x <= y));
System.out.println("x == y : " + (x == y));
System.out.println("x != y : " + (x != y));
} Output:
} x > y : true
x < y : false
x >= y : true
x <= y : false
x == y : false
x != y : true
4. Logical Operators
Logical operators return a true or false value based on the state of the Variables. Given
that x and y represent boolean expressions, the boolean logical operators are defined
in the Table below.

x y !x x && y x || y x^y
true true false true true false
true false false false true true
false true true false true true

false false true false false false


Example Program:
public class LogicalOperatorsDemo
{
public static void main(String args[])
{
boolean x = true;
boolean y = false;
System.out.println("x & y : " + (x & y));
System.out.println("x && y : " + (x && y));
System.out.println("x | y : " + (x | y));
System.out.println("x || y: " + (x || y)); Output:
System.out.println("x ^ y : " + (x ^ y)); x & y : false
System.out.println("!x : " + (!x)); x && y : false
} x | y : true
} x || y: true
x ^ y : true
!x : false
5. Bitwise Operators
Java provides Bit wise operators to manipulate the contents of variables at the bit level.
The result of applying bitwise operators between two corresponding bits in the operands
is shown in the Table below.

x y ~x x&y x|y x^y


1 1 0 1 1 0
1 0 0 0 1 1
0 1 1 0 1 1
0 0 1 0 0 0
public class Test
{
public static void main(String args[])
{
int a = 5; // Added missing semicolon 0000 0101 (5)
int b = 4; // Fixed comment alignment for clarity & 0000 0100 (4)
int c, d; ---------------
c = a & b; // Bitwise AND operation 0000 0100 (4)
d = a | b; // Bitwise OR operation
System.out.println("a & b = " + c );
System.out.println("a | b = " + d );
}
}
0000 0101 (5)
| 0000 0100 (4)
Output:
--------------
a&b=4
0000 0101 (5)
a|b=5
6. Compound Assignment operators
The compound operators perform shortcuts in common programming operations.
Java has eleven compound assignment operators.

Syntax:
Example Program:
variable1 operator = variable2.
class OperatorExample
Ex. {
x+=y public static void main(String[] args)
x*=y {
int a = 10;
a += 3; // a = a + 3
System.out.println(a);
a -= 4; // a = a - 4
System.out.println(a);
a *= 2; // a = a * 2 Output:
System.out.println(a); 13
a /= 2; // a = a / 2 9
System.out.println(a); 18
}} 9
7. Conditional Operators
The Conditional operator is the ternary (operator takes three arguments) operator in Java.
The operator evaluates the first argument and, if true, evaluates the second argument.
If the first argument evaluates to false, then the third argument is evaluated. The
conditional operator is the expression equivalent of the if-else statement.

public class TernaryOperatorsDemo


{
public static void main(String[] args)
{
{ int x = 10,
y = 12, z = 0;
z = x > y ? x : y;
System.out.println("z : " + z); }
} Output:
} 12
8. Instance of Operator:
The instanceof operator in Java is used to test whether an object is an instance of a
specific class or subclass, or if it implements a particular interface.

Syntax:
object instanceof ClassName Dog d = new Dog();
Animal ad = new Dog();
class Animal
System.out.println(a instanceof Animal);
{
System.out.println(d instanceof Dog);
}
System.out.println(d instanceof Animal);
class Dog extends Animal
System.out.println(ad instanceof Dog);
{
System.out.println(a instanceof Dog);
}
}
public class InstanceOfExample Output:
}
{
true
public static void main(String[] args)
true
{
Animal a = new Animal();
true
True
false

You might also like