OOP CS3391 UNIT-1[Lecture note-13]
OOP CS3391 UNIT-1[Lecture note-13]
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:
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
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.
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