Skip to main content

Operators

Operators:-operators are used to perform some logical or mathematical operations.
Types of Operators:-
1.     Arithmetic Operator
(a)  Binary Arithmetic
(b) Unary Arithmetic
2.     Relational Operator
3.     Logical operator
4.     Bitwise operator
5.     Ternary or conditional operator
6.     instanceof  operator
7.     comma operator
8.     assignment operator
9.     dot operator
Binary Arithmetic:-Binary arithmetic operator operates on two operands. Operators are +,-,*, /, %.example like 5+2, 5-2, 5*2, 5/2, 5%2.
Example1:- TestOperator.java
class TestOperator
{
public static void main(String[]args)
{
int a=10,b=5;
System.out.println("Addition:"+(a+b));
System.out.println("Subtraction:"+(a-b));
System.out.println("Multiplication:"+(a*b));
System.out.println("Division:"+(a/b));
System.out.println("Remainder:"+(a%b));
}
}
Output:-
C:\JAVATECH>javac TestOperator.java
C:\JAVATECH>java TestOperator
Addition:15
Subtraction:5
Multiplication:50
Division:2
Remainder:0
Example2:- TestOperator.java
import java.util.*;
class TestOperator
{
public static void main(String[]args)
{
Scanner s=new Scanner(System.in);
System.out.println("enter two number");
int a=s.nextInt();
int b=s.nextInt();
System.out.println("Addition:"+(a+b));
System.out.println("Subtraction:"+(a-b));
System.out.println("Multiplication:"+(a*b));
System.out.println("Division:"+(a/b));
System.out.println("Remainder:"+(a%b));
}
}
Output:-
C:\JAVATECH>javac TestOperator.java
C:\JAVATECH>java TestOperator
enter two number
551
12
Addition:563
Subtraction:539
Multiplication:6612
Division:45
Remainder:11
Unary operator:-unary operator operates on single operand or single variable.
Operators are: ++,--
++ Operator also known as increment (increment by one) operator and – called decrement (decrement by one) operator.
We can write unary operator before operand and after operand.
Like ++a     and    a++.
++a is a pre increment that means first increment in variable a then use variable a and a++ is a post increment operator that means first use variable a then increment into variable a.

Example1:- TestOperator.java
class TestOperator
{
public static void main(String[]args)
{
int i=1;
System.out.println(i++);
System.out.println(++i);
System.out.println(i++);
System.out.println(++i);
}
}
Output:-
C:\JAVATECH>javac TestOperator.java
C:\JAVATECH>java TestOperator
1
3
3
5
Example2:- TestOperator.java
class TestOperator
{
public static void main(String[]args)
{
int i=1,j;
j=i++;
System.out.println("i     J");
System.out.println(i+"     "+j);
i=1;
j=++i;
System.out.println(i+"     "+j);
i=1;
i=(++i)+(i++)+(++i)+(++i);
System.out.println(i);
i=1;
j=(++i)+(i++)+(++i)+(++i);
System.out.println(i+"     "+j);
}
}
Output:-
C:\JAVATECH>javac TestOperator.java
C:\JAVATECH>java TestOperator
i     J
2     1
2     2
13
5     13
Relational Operator:-relational operators are used for some comparison type of operations. Relational operator returns boolean type of value, it returns true if the given expression is true otherwise returns false.

Operator
Meaning
example
result
< 
Less than
5<6
true
> 
Greater than
5>6
false
<=
Less than or equal to
5<=6
true
>=
Greater than or equal to
5>=6
false
==
Equal to equal to
5==6
false
!=
Not equal to
5!=6
true

Example1:- TestOperator.java
class TestOperator
{
public static void main(String[]args)
{
System.out.println(5<6);
System.out.println(5>6);
System.out.println(5<=6);
System.out.println(5>=6);
System.out.println(5==6);
System.out.println(5!=6);
}
}
Output:-
C:\JAVATECH>javac TestOperator.java
C:\JAVATECH>java TestOperator
true
false
true
false
false
true
Example2:- TestOperator.java
class TestOperator
{
public static void main(String[]args)
{
System.out.println(5<'A');
System.out.println(5>13.90);
System.out.println(5<true);
System.out.println(true>false);
}
}
Output:-
C:\JAVATECH>javac TestOperator.java
TestOperator.java:7: error: bad operand types for binary operator '<'
System.out.println(5<true);
                    ^
  first type:  int
  second type: boolean
TestOperator.java:8: error: bad operand types for binary operator '>'
System.out.println(true>false);
                       ^
  first type:  boolean
  second type: boolean
2 errors
Logical operator:-logical operator operates on relational operator and returns true or false just like relational operator.
Operators are:-
Logical AND (&&)
Logical OR (||)
Logical NOT (!)
Example:-  
((a<b)&&(b>c))         ((a<b)||(b>c))           !(a<b)
Condition 1
Condition 2
Condition 1&& Condition 2
T
T
T
T
F
F
F
T
F
F
F
F


                      


Condition 1
Condition 2
Condition 1|| Condition 2
T
T
T
T
F
T
F
T
T
F
F
F


                      

Condition
!Condition
T
F
F
T


                      

Example1:- TestOperator.java
class TestOperator
{
public static void main(String[]args)
{
System.out.println(((5<6)&&(50>40)));
System.out.println(((5>6)||(50>40)));
System.out.println(!(5<6));
}}
Output:-
C:\JAVATECH>javac TestOperator.java
C:\JAVATECH>java TestOperator
true
true
false
Bitwise operator:-bitwise operator operates on bits.
Types of bitwise operator:-
1.      Bitwise AND (&) 
2.     Bitwise OR(|)
3.     Bitwise XOR(^)
4.     Bitwise negation(~)
5.     Bitwise left shift(<<)
6.     Bitwise right shift(>>)
7.     Bitwise right shift with zero fill(>>>)
Bitwise OR (|):-bitwise operator operates on binary bits, in case of bitwise OR operator result will be 0 if both bits are 0 otherwise result will be 1.
Example: - suppose a=18 and b=25 then a|b will be as below
Decimal
Binary
18
1 0 0 1 0
25
1 1 0 0 1
         27(a|b)
1 1 0 1 1

Bitwise XOR (^):-bitwise operator operates on binary bits, in case of bitwise XOR operator result will be 0 if both bits are same otherwise result will be 1.
Example: - suppose a=18 and b=25 then a^b will be as below
Decimal
Binary
18
1 0 0 1 0
25
1 1 0 0 1
         11(a^b)
0 1 0 1 1

Bitwise Negation:-bitwise negation provide 1’s complement of given number that means convert 1 to 0 and 0 to 1.for finding the result we have to get 2’s complement of that number.
Bitwise left shift (<<):-it is used to shift bits in right to left direction, new bits will inserted from right side and bits will shift to left direction.
Like    a<<2 that means binary value of variable a will shift 2 bit left. If a is positive then inserted bit will be 0 if a is negative then inserted bit will be 1.
Suppose   byte a=18   so binary value of a will be
0 0 0 1 0 0 1 0  <---00
0 1 0 0 1 0 0 0  (72 in decimal)
Bitwise right shift (>>):-it is used to shift bits in left to right  direction, new bits will inserted from left side and bits will shift to right direction.
Like    a>>2 that means binary value of variable a will shift 2 bit right. If a is positive then inserted bit will be 0 if a is negative then inserted bit will be 1.
Suppose   byte a=18   so binary value of a will be
00---à0 0 0 1 0 0 1 0
0 0 0 0 0 1 0 0  (4 in decimal)
Bitwise right shift with zero fill (>>>):-it is used to shift bits in left to right  direction, new bits will inserted from left side and bits will shift to right direction.
Like    a>>>2 that means binary value of variable a will shift 2 bit right. Inserted bit will be always 0.
Suppose   byte a=18   so binary value of a will be
00---à0 0 0 1 0 0 1 0
0 0 0 0 0 1 0 0  (4 in decimal)
Example1:- TestOperator.java
class TestOperator
{
public static void main(String[]args)
{
System.out.println(18&25);
System.out.println(18|25);
System.out.println(18^25);
System.out.println(~18);
System.out.println(18<<2);
System.out.println(18>>2);
System.out.println(18>>>2);
}
}
Output:-
C:\JAVATECH>javac TestOperator.java
C:\JAVATECH>java TestOperator
16
27
11
-19
72
4
4
Ternary or conditional operator:-conditional or ternary operator is used to take decision if condition is true or false.
Example1:- TestOperator.java
class TestOperator
{
public static void main(String[]args)
{
int a=100,b=20,c;
c=(a>b)?a:b;
System.out.println("greater no"+c);
}
}
Output:-
C:\JAVATECH>javac TestOperator.java
C:\JAVATECH>java TestOperator
greater no100
instanceof:- instanceof operator is used for finding relationship between object and class or for finding particular object of particular class.
Example:-TestOperator.java
class A
{
}
class B
{
}
class TestOperator
{
public static void main(String[]args)
{
A a=new A();
B b=new B();
if(a instanceof A)
{
System.out.println("Object of class A ");
}
if(b instanceof B)
{
System.out.println("Object of class B ");
}}}
Output:-
C:\JAVATECH>javac TestOperator.java
C:\JAVATECH>java TestOperator
Object of class A

Object of class B

Comments

Popular posts from this blog

Inner classes

Inner Classes:- class inside class or interface called inner class or nested class. Advantage of inner classes is grouping multiple classes and interfaces into a single class. Another big advantage of inner class is that inner class can happily access all the data or variables of outer class directly. Types of Nested classes:- There are two types of nested classes static and non-static nested classes. (A)Non –static inner classes:- 1)     Member inner class 2)     Anonymous inner class 3)     Local inner class (B)Static nested class Example1:-Outer.java class Outer { class Inner { void disp() { System.out.println("Hello This is Inner class Method"); } } public static void main(String[]args) { Inner in=new Inner(); in.disp(); } } Output:- C:\JAVATECH>javac Outer.java Outer.java:12: error: non-static variable this cannot be referenced from a static context Inner in=new Inner(); ...

Inheritance-4

Example10:- class Student { Student() { System.out.println("This is super class Zero argument constructor"); } Student(String s) { System.out.println("This is super class One argument constructor"); } } class Test extends Student { Test() { super(null); System.out.println("This is sub class Zero argument constructor"); } public static void main(String[]args) { Test t=new Test(); } } Output:- C:\JAVATECH>javac Test.java C:\JAVATECH>java Test This is super class One argument constructor This is sub class Zero argument constructor Example11:- class Student { Student() { System.out.println("This is super class Zero argument constructor"); } Student(String s) { System.out.println("This is super class One argument constructor"); } } class Test extends Student { Test() { super(); super(null); System.out.println("This is su...

Object,Data Hiding

Object:- Object is real time entity exist in the real world , the basic purpose of object is to transfer data from one location to another location.object is small memory , allocated inside primary memory(RAM).in java object created inside heap memory,actually in java RAM memory  is divided into some segment according to the type of variables for example. RAM==>Heap area+Stack area+String constant pool+class area  etc. when object created then inside object only non static class variables occupied memory and initialize with default values. non static class variables also known as instance variable reside into object in heap area,static variables occupied memory in class area at the time of class loading from HARD disk to RAM and all local variables occupied memory in stack area when reaching to variables. object  created from the class then what object contains is depend on class structure or class members.             ...