Professional Documents
Culture Documents
Operators
Operators
Operators
- Subtraction operator
* Multiplication operator
/ Division operator
% Remainder operator
Relational Operators
== equal to
!= not equal to
• If we use the || and && forms, rather than the | and & forms
of these operators, Java will not bother to evaluate the right-
hand operand when the outcome of the expression can be
determined by the left operand alone.
This is very useful when the right-hand operand depends
on the value of the left one in order to function properly.
For example
if (denom != 0 && num / denom > 10)
General format-
object instanceof class/subclass/interface
class Simple1{
public static void main(String args[]){
Simple1 s=new Simple1();
System.out.println(s instanceof Simple1);
}
}
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);
}
}
class Dog2{
public static void main(String args[]){
Dog2 d=null;
System.out.println(d instanceof Dog2);
}
}
class Operators
{
public static void main(String[] args)
{
Person obj1 = new Person();
Person obj2 = new Boy();
System.out.println("obj1 instanceof Person: " + (obj1 instanceof Person));
System.out.println("obj1 instanceof Boy: " + (obj1 instanceof Boy));
System.out.println("obj1 instanceof MyInterface: " + (obj1 instanceof
MyInterface));
System.out.println("obj2 instanceof Person: " + (obj2 instanceof Person));
System.out.println("obj2 instanceof Boy: " + (obj2 instanceof Boy));
System.out.println("obj2 instanceof MyInterface: " + (obj2 instanceof
MyInterface));
}}
class Person { }
class Boy extends Person implements MyInterface { }
interface MyInterface { }
Programs in java
• Calculator Program
• Factorial Program
• Palindrome Program
• String reverse
The Conditional Operator (?:)
• Java includes a special ternary (three-way)operator, ?, that can
replace certain types of if-then-else statements.
• The ? has this general form:
expression1 ? expression2 : expression3
• Here,expression1 can be any expression that evaluates to a
boolean value.
•
The Left Shift Operator
The decimal representation of a number is a
base-10 number system having only ten states 0,
1, 2, 3, 4, 5, 6, 7, 8, and 9. For example, 4, 10,
16, etc.
• Example:
01000001 65 << 2
00000100 4
The Right Shift Operator
• The right shift operator, >>, shifts all of the bits in a value
to the right a specified number of times.
value >> num
• It is also known as signed right shift.
• Example:
00100011 35 >> 2
00001000 8
• When we are shifting right, the top (leftmost) bits exposed
by the right shift are filled in with the previous contents of
the top bit.
• Example:
– 11111111 11111111 11111111 11111111 –1 in
binary as an int
– >>>24
– 00000000 00000000 00000000 11111111 255 in
binary as an int
Operator Precedence
Brainstorming 1
What will be the output of the following code snippets?
System.out.println(34>>3);
System.out.println(-34>>3);
System.out.println(-34>>>3);
Brainstorming 2
int x = 10, y = 5;
while(x- - > 7 || + + y < 8 );
System.out.print(x);
System.out.print(y);
A. 95
B. 67
C. 78
D. 48
E. 77
F. N.O.T
Brainstorming 3
System.out.print(2>1||4>3?false:true);
class X{}
class Y extends X{}
class Z extends Y{}
X x1 = new Y();
Y y1 = new Z();
Y y2 = new Y();
System.out.println( x1 instanceof X);
System.out.println( x1 instanceof Z);
System.out.println( y1 instanceof Z);
System.out.println( y2 instanceof X);
Brainstorming 4
System.out.print(2>1||4>3?false:true);