0% found this document useful (0 votes)
29 views39 pages

Operators Day 2

The document provides a comprehensive overview of Java operators, including examples of swapping numbers using various methods, operator types, and operator precedence. It also covers unary, arithmetic, bitwise, logical, ternary, and assignment operators, along with practical examples and outputs. Additionally, it includes multiple-choice questions and a sample program for comparing two integers.

Uploaded by

gothamponnusamy
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)
29 views39 pages

Operators Day 2

The document provides a comprehensive overview of Java operators, including examples of swapping numbers using various methods, operator types, and operator precedence. It also covers unary, arithmetic, bitwise, logical, ternary, and assignment operators, along with practical examples and outputs. Additionally, it includes multiple-choice questions and a sample program for comparing two integers.

Uploaded by

gothamponnusamy
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

JAVA

Operators
1. Write a Java program to swap two numbers with and without
using third variable.
5. Using arithmetic operators *
Logic and / in one line
1. Simple Logic a = (a * b) / (b = a);
temp = a; 6. Using arithmetic operators +
a = b; and - in one line
b = temp; a = (a + b) - (b = a);
2. Using arithmetic operators + and - 7. Using arithmetic operators +
a = a + b; and - in a slightly different way
b = a - b; a = b - a;
a = a - b; b = b - a;
3. Using arithmetic operators * and /
a = b + a;
a = a * b;
8. int main(int argc, char **argv)
b = a / b;
{
a = a / b;
4. Using bitwise XOR operator ^ int a = 10, b = 5;
a = a ^ b; argc = a;
b = a ^ b; a = b;
a = a ^ b; b = argc;
printf("After swapping: a = %d,
b = %d", a, b); return 0; }
Operators
Operator is a symbol that is used to perform operations.
There are many types of operators in java
 Unary Operator,
 Arithmetic Operator,
 Shift Operator,
 Relational Operator,
 Bitwise Operator,
 Logical Operator,
 Ternary Operator and
 Assignment Operator.
Java Operator Precedence
Operator Type Category Precedence
Unary postfix expr++ expr--
prefix ++expr --expr +expr -expr ~ !
Arithmetic multiplicative */%
additive +-
Shift shift << >> >>>
Relational comparison < > <= >= instanceof
equality == !=
Bitwise bitwise AND &
bitwise exclusive OR ^
bitwise inclusive OR |
Logical logical AND &&
logical OR ||
Ternary ternary ?:
Assignment assignment = += -= *= /= %= &= ^= |= <<= >>=
>>>=
Java Unary Operator
Java Unary Operator Example: ++ and --
class OperatorExample{
public static void main(String args[]){
int x=10;
System.out.println(x++);
System.out.println(++x);
System.out.println(x--);
System.out.println(--x);
}
}
Output:
10 12 12 10
Java Unary Operator Example 2: ++ and --
class OperatorExample{
public static void main(String args[]){
int a=10;
int b=10;
System.out.println(a++ + ++a);
System.out.println(b++ + b++);
}
}

Output:
22 21
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;
System.out.println(~a);//-
11 (minus of total positive value which starts from 1)
System.out.println(~b);//
9 (positive of total minus, positive starts from 0)
System.out.println(!c);
System.out.println(!d);
}}
Output:
-11 9 false true
Java Arithmetic Operator Example
class OperatorExample{
public static void main(String args[]){
int a=10;
int b=5;
System.out.println(a+b);
System.out.println(a-b);
System.out.println(a*b);
System.out.println(a/b);
System.out.println(a%b);
}}
Output:
15 5 50 2 0
Java Arithmetic Operator Example: Expression
class OperatorExample{
public static void main(String args[]){
System.out.println(10*10/5+3-1*4/2);
}}

Output:
21
Java Left Shift Operator Example
class OperatorExample{
public static void main(String args[]){
System.out.println(10<<2);//10*(2^2)=10*4=40
System.out.println(10<<3);
System.out.println(20<<2);
System.out.println(15<<4);
}}
Output:
40
80
80
240
Java Right Shift Operator Example
class OperatorExample{
public static void main(String args[]){
System.out.println(10>>2);//10/(2^2)=10/4=2
System.out.println(20>>2);
System.out.println(20>>3);
}}
Output:
2
5
2
Java Shift Operator Example: >> vs >>>
class OperatorExample{
public static void main(String args[]){

System.out.println(20>>2);
System.out.println(20>>>2);
//For negative number, >>> changes parity bit (MSB) to 0
System.out.println(-20>>2);

}}
Output:
5
5
-5
Java AND Operator Example:
Logical && and Bitwise &
The logical && operator doesn't check second condition if first
condition is false. It checks second condition only if first one is
true.
The bitwise & operator always checks both conditions whether
first condition is true or false.
class OperatorExample{
public static void main(String args[]){
int a=10;
int b=5;
int c=20;
System.out.println(a<b&&a<c);
System.out.println(a<b&a<c);
}}
Output:
false
Java AND Operator Example:
Logical && vs Bitwise &
class OperatorExample{
public static void main(String args[]){
int a=10;
int b=5;
int c=20;
System.out.println(a<b&&a++<c);
System.out.println(a);
System.out.println(a<b&a++<c);
System.out.println(a);
}}
Output:
false
10
false
11
Java OR Operator Example:
Logical || and Bitwise |
The logical || operator doesn't check second condition if first
condition is true. It checks second condition only if first one is
false.
The bitwise | operator always checks both conditions whether first
condition is true or false.
class OperatorExample{
public static void main(String args[]){
Output:
int a=10;
true true true 10 true 11
int b=5;
int c=20;
System.out.println(a>b||a<c);
System.out.println(a>b|a<c);
System.out.println(a>b||a++<c);//true || true = true
System.out.println(a);//
10 because second condition is not checked
System.out.println(a>b|a++<c);//true | true = true
Java Ternary Operator
Java Ternary operator is used as one liner replacement for if-then-
else statement and used a lot in java programming. it is the only
conditional operator which takes three operands.
Java Ternary Operator Example

class OperatorExample{
public static void main(String args[]){
int a=2;
int b=5;
int min=(a<b)?a:b;
System.out.println(min);
}
}

Output:
2
Another Example:
class OperatorExample{
public static void main(String args[]){
int a=10;
int b=5;
int min=(a<b)?a:b;
System.out.println(min);
}
}

Output:
5
Java Assignment Operator Example
class OperatorExample{
public static void main(String args[]){
int a=10;
int b=20;
a+=4;
b-=4;
System.out.println(a);
System.out.println(b);
}}

Output:
14
16
Java Assignment Operator Example
class OperatorExample{
public static void main(String[] args){
int a=10;
a+=3;
System.out.println(a);
a-=4;
System.out.println(a);
a*=2;
System.out.println(a);
a/=2;
System.out.println(a);
}}
Output:
13 9 18 9
Java Assignment Operator Example:
Adding short
class OperatorExample{
public static void main(String args[]){
short a=10;
short b=10;
a=a+b;
System.out.println(a);
}}
Output:
Compile time error
After type cast:
class OperatorExample{
public static void main(String args[]){
short a=10;
short b=10;
a=(short)(a+b);
System.out.println(a);
}}

Output:
20
Java instanceof or type comparison operator

• The java instanceof operator is used to test whether the


object is an instance of the specified type (class or subclass or
interface).
• It returns either true or false. If we apply the instanceof
operator with any variable that has null value, it returns false.
class Simple1{
public static void main(String args[]){
Simple1 s=new Simple1();
System.out.println(s instanceof Simple1);//true
}
}
Output:true
• An object of subclass type is also a type of parent class. For
example, if Dog extends Animal then object of Dog can be
referred by either Dog or Animal class.

class Animal{}
class Dog1 extends Animal{//Dog inherits Animal

public static void main(String args[]){


Dog1 d=new Dog1();
System.out.println(d instanceof Animal);//true
}
}
Output:true
class Dog2{
public static void main(String args[]){
Dog2 d=null;
System.out.println(d instanceof Dog2);//false
}
}
Output:false
mcqs
1. What is the output of this program?
class ternary_operator
{
public static void main(String args[])
{
int x = 3;
int y = ~ x;
int z;
z = x > y ? x : y;
System.out.print(z);
}
}
a) 0
b) 1
c) 3
d) -4
2. What is the output of this program?
class Output
{
public static void main(String args[])
{
int x , y = 1;
x = 10;
if (x != 10 && x / 0 == 0)
System.out.println(y);
else
System.out.println(++y);
}
}
a) 1
b) 2
c) Runtime error owing to division by zero in if condition
d) Unpredictable behaviour of program
3. What is the output of following Java Program
class Test {
public static void main(String args[]) {
int x = -4;
System.out.println(x>>1);
int y = 4;
System.out.println(y>>1);
}
}
(A) Compiler Error: Operator >> cannot be applied to negative numbers
(B)-2
2
(C) 2
2
(D) 0
2
5. int x = 0, y = 0 , z = 0 ;
x = (++x + y-- ) * z++;
What will be the value of "x" after execution ?
A. -2
B. -1
C. 0
D. 1

6. int ++a = 100 ;


System.out.println( ++a ) ;
What will be the output of the above fraction of code ?
A. 100
B. Displays error as ++a is not enclosed in double quotes in println statement
C. Compiler displays error as ++a is not a valid identifier
D. None of these
7. What will be the output?
if(1 + 1 + 1 + 1 + 1 == 5){
System.out.print("TRUE");
}
else{
System.out.print("FLASE");
}
A. TRUE
B. FALSE
C. Compiler Error
D. None of these
8. public class Test{
public static void main(String args[]){
System.out.print(""=="");
System.out.print(" ");
System.out.print("A"=="A");
System.out.print(" ");
System.out.print("a==A");
}

}
A. "==" A"=="A a==A
B. true true false
C. true true a==A
D. Compilation Fails
E. None of the above
9. What will be the output?
public class Test{
public static void main(String args[]){
int a = 42;
double b = 42.25;
System.out.print((a%10)+" "+(b%10));
}
}
A. 42 42.5
B. 2 2.5
C. 4.2 4.225
D. 2 4.225
E. Compilation Error
10. What will be the output after compiling and running following code?
1. public class Test{
2. public static void main(String... args){
3. int x =5;
4. x *= 3 + 7;
5. System.out.println(x);
6. }
7. }

A. 22
B. 50
C. 10
D. Compilation fails with an error at line 4
Answers
1. C-3
2. B-2
3. B - -2 2
4. D - none of these
5. C-0
6. C – compiler display error
7. A - true
8. C – true true a==A
9. B – 22.5
10. B - 50
1. Write a Java program to compare two numbers.
Test Data:
Input first integer: 25
Input second integer: 39
import java.util.Scanner;
public class Exercise32 {
public static void main( String args[] )
{
// create Scanner to obtain input from command window
Scanner input = new Scanner(System.in);
int number1; // first number to compare
int number2; // second number to compare

System.out.print( "Input first integer: " ); // prompt


number1 = input.nextInt(); // read first number from user

System.out.print( "Input second integer: " ); // prompt


number2 = input.nextInt(); // read second number from user
if ( number1 == number2 )
System.out.printf( "%d == %d\n", number1, number2 );
if ( number1 != number2 )
System.out.printf( "%d != %d\n", number1, number2 );
if ( number1 < number2 )
System.out.printf( "%d < %d\n", number1, number2 );
if ( number1 > number2 )
System.out.printf( "%d > %d\n", number1, number2 );
if ( number1 <= number2 )
System.out.printf( "%d <= %d\n", number1, number2 );
if ( number1 >= number2 )
System.out.printf( "%d >= %d\n", number1, number2 );
}
}
Sample Output
Input first integer: 25

Input second integer: 39

25 != 39
25 < 39
25 <= 39
Thank you

You might also like