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

Introduction of Java

Java is a high level programming language which is used for creating various types of software's or applications. Types of java applications:- 1)     Stand alone application 2)     Desktop based application          3)     Web based application 4)     Enterprise application etc. Stand alone application:- Stand alone application run on single machine without user interface and every stand alone application having main method. Desktop based application:- Desktop based application also run on single machine with user interface and every Desktop based application having main method. Web based application:- Web based application run on web, web based application not required main method but for such types of application need server. Enterprise application:- Enterprise application is category of web application where doing some business or involve some money related issues. Java Editions: - There are four Java platforms. ·       

Small concept about OOPs

Some small concept about OOPs:- In many books and website write about OOPs concepts as hundred percent object oriented, pure object oriented and partial object oriented language. Hundred percent object oriented:- In the hundred percent object oriented movie hero is object, without object program is incomplete that means can not execute program without object. Without class and object programming or application development is not possible. Example of hundred percent object oriented language is  Smalltalk . Pure object oriented:- In the object oriented cinema obviously object is the hero. In pure object oriented language object having same importance but in this having some relax that is without creating object we can successfully execute our application. So in the object oriented programming without class we cannot create or run our application, class is must. Example of pure object oriented language is  Java . Partial object oriented:- In this type of language cl

java print

Syntax of main method in java is    public static void main(String[]args)  before understanding about main method we have to understand that who is responsible for main method.if we write a simple program or write a simple java class without main method like      Test.java           class Test      {      } above program will compile successfully.for compilation of  java file use  javac tool  or  javac.exe  file. javac Test.java it means compiler checks only syntax of class that means compiler does not check the main method in the file or program,so that compiler is not responsible for main method in java. After successfully compilation of above program we have to run the generated byte code. java Test Error: Main method not found in class Test, please define the main method as:    public static void main(String[] args) So JVM (java virtual machine) or java interpreter is responsible for main method,first jvm check that the main method is pres