• iconJava Online Training In Andhra Pradesh and Telangana
  • icon9010519704

Opening Hours :7AM to 9PM

Operators


Operator
Operator is a symbol which are used to perform particular operation. In java language these opeatrs are classified into following types.
1.Unary Operators
2.Binary Opraators
3.Ternary Oprators

Unary Operator:
If any operation perform on single operand (Variable) is known as uanry operrator.
These opeators are classified into following types.
1.Unary Minus Operator
2.Incremnent Operator
3.Decrement Operator

1.Unary Minus:
Unary minus means sign value or minus value
Example:
int x=10;
then -x=-10;

class Main
{
public static void main(String args[])
{
int x=10;
System.out.println("The Sign value is "+(-x));
}
}


Output:
The Sign value is -10

class Main
{
public static void main(String args[])
{
int x;
x=Integer.parseInt(args[0]);
System.out.println("The Sign value is "+(-x));
}
}


Output:
The Sign value is -10

import java.util.*;
class Main
{
public static void main(String args[])
{
int x;
Scanner s=new Scanner(System.in);
System.out.println("Enter x Value");
x=s.nextInt();
System.out.println("The Sign value is "+(-x));
}
}


Output:
Enter x Value
The Sign value is -10
Increment Operator:
Increment Operator is used to increment the one value to the current value. These are classified into following types.
1.PreIncrement operator
2.PostIncrement operator

PreIncrement Operator(++x):

PreIncrement oprator is used to increment the value first assign the value later. Example:
int x=10;
then ++x=11;

class Main
{
public static void main(String args[])
{
int x=10;
System.out.println("The PreIncrement value is "+(++x));
}
}


Output:
The PreIncrement value is 11

class Main
{
public static void main(String args[])
{
int x;
x=Integer.parseInt(args[0]);
System.out.println("The PreIncrement value is "+(++x));
}
}


Output:
The PreIncrement value is 11

import java.util.*;
class Main
{
public static void main(String args[])
{
int x;
Scanner s=new Scanner(System.in);
System.out.println("Enter x Value");
x=s.nextInt();
System.out.println("The PreIncrement value is "+(++x));
}
}


Output:
Enter x Value
The PreIncrement value is 11
Post Increment Operator:
PostIncrement Operator(x++): In the post incrementation assign the value first increment the value later.
Example:
int x=10;
then x++=10;

class Main
{
public static void main(String args[])
{
int x=10;
System.out.println("The PostIncrement value is "+(x++));
}
}


Output:
The PostIncrement value is 10

class Main
{
public static void main(String args[])
{
int x;
x=Integer.parseInt(args[0]);
System.out.println("The PostIncrement value is "+(x++));
}
}


Output:
The PostIncrement value is 10

import java.util.*;
class Main
{
public static void main(String args[])
{
int x;
Scanner s=new Scanner(System.in);
System.out.println("Enter x Value");
x=s.nextInt();
System.out.println("The PostIncrement value is "+(x++));
}
}


Output:
Enter x Value
The PostIncrement value is 10
Decrement Operator:
Decrement Operator is used to decrement the one value to the current value. These are classified into following types.
1.PreDecrement operator
2.PostDecrement operator

PreDecrement Operator(--x):
PreDecrement oprator is used to Decrement the value first assign the value later. Example:
int x=10;
then --x=9;

class Main
{
public static void main(String args[])
{
int x=10;
System.out.println("The PreDecrement value is "+(--x));
}
}


Output:
The PreDecrement value is 9

class Main
{
public static void main(String args[])
{
int x;
x=Integer.parseInt(args[0]);
System.out.println("The PreDecrement value is "+(--x));
}
}


Output:
The PreDecrement value is 9

import java.util.*;
class Main
{
public static void main(String args[])
{
int x;
Scanner s=new Scanner(System.in);
System.out.println("Enter x Value");
x=s.nextInt();
System.out.println("The PreDecrement value is "+(--x));
}
}


Output:
Enter x Value
The PreDecrement value is 9
Post Decrement Operator:
PostDecrement Operator(x--): In the post Decrement assign the value first decrement the value later.
Example:
int x=10;
then x++=10;

class Main
{
public static void main(String args[])
{
int x=10;
System.out.println("The PostDecrementvalue is "+(x--));
}
}


Output:
The PostDecrement value is 10

class Main
{
public static void main(String args[])
{
int x;
x=Integer.parseInt(args[0]);
System.out.println("The PostDecrement value is "+(x--));
}
}


Output:
The PostDecrement value is 10

import java.util.*;
class Main
{
public static void main(String args[])
{
int x;
Scanner s=new Scanner(System.in);
System.out.println("Enter x Value");
x=s.nextInt();
System.out.println("The PostDecrement value is "+(x--);
}
}


Output:
Enter x Value
The PostDecrement value is 10

Binary Operators


Binary Operator:
If any operation perform on two operands(variable) is known as binary operators.
In java language these opeatrs are classified into following types.
1.Arithmetic Operators
2.Assignment Operators
3.Relational Operators
4.Logical Operators
5.Bitwise Operators

Arithmetic Operators
These are the operands can be used to perform simple mathametical operation. Which are classified into following types.
Operator Operation Example
+ Addition x+y
- Substraction x-y
* Multiplication x*y
/ Division x/y
% Modular Division x%y

Addition Of Two Numbers

class Main
{
public static void main(String args[])
{
int x=10,y=20,z;
z=x+y;
System.out.println("Addition is "+(z));
}
}

Output:
Addition is 30
Addition Of Two Numbers Using Commandline Arguments

class Main
{
public static void main(String args[])
{
int x,y,z;
x=Integer.parseInt(args[0]);
y=Integer.parseInt(args[1]);
z=x+y;
System.out.println("Addition is "+(z));
}
}

Output:
Addition is 30
Addition Of Two Numbers Using Scanner

import java.util.*;
class Main
{
public static void main(String args[])
{
int x,y,z;
Scanner s=new Scanner(System.in);
System.out.println("Enter x,y values");
x=s.nextInt();
y=s.nextInt();
z=x+y;
System.out.println("Addition is "+(z));
}
}

Output:
Addition is 30
Assignment Operators
Assignment operators are used to assign the values to the variable or assign the variable to variable
There are classified into following types.
Operator Example Same As
= x=y x=y
+= x+=y x=x+y
-= x-=y x=x-y
*= x*=y x=x*y
/= x/=y x=x/y
%= x%=y x=x%y

Swaping Of Two Numbers With Using Third Variable

class Main
{
public static void main(String args[])
{
int x=10,y=20,z;
z=x;
x=y;
y=z;
System.out.println("x value is: "+(x));
System.out.println("y value is: "+(y));
}
}

Output:
x value is: 20
y value is: 10
Swaping Of Two Numbers Using Commandline Arguments

class Main
{
public static void main(String args[])
{
int x,y,z;
x=Integer.parseInt(args[0]);
y=Integer.parseInt(args[1]);
z=x;
x=y;
y=z;
System.out.println("x value is: "+(x));
System.out.println("y value is: "+(y));
}
}

Output:
x value is: 20
y value is: 10
Swaping Of Two Numbers Using Scanner

import java.util.*;
class Main
{
public static void main(String args[])
{
int x,y,z;
Scanner s=new Scanner(System.in);
System.out.println("ENter x,y values");
x=s.nextInt();
y=s.nextInt();
z=x;
x=y;
y=z;
System.out.println("x value is: "+(x));
System.out.println("y value is: "+(y));
}
}

Output:
x value is: 20
y value is: 10
Swaping Of Two Numbers Without Using Third Variable

Swaping Of Two Numbers Without Using Third Variable

class Main
{
public static void main(String args[])
{
int x=10,y=20;
x=x+y;
y=x-y;
x=x-y;
System.out.println("x value is: "+(x));
System.out.println("y value is: "+(y));
}
}

Output:
x value is: 20
y value is: 10
Swaping Of Two Numbers Using Commandline Arguments

class Main
{
public static void main(String args[])
{
int x,y,z;
x=Integer.parseInt(args[0]);
y=Integer.parseInt(args[1]);
x=x+y;
y=x-y;
x=x-y;
System.out.println("x value is: "+(x));
System.out.println("y value is: "+(y));
}
}

Output:
x value is: 20
y value is: 10
Swaping Of Two Numbers Using Scanner

import java.util.*;
class Main
{
public static void main(String args[])
{
int x,y,z;
Scanner s=new Scanner(System.in);
System.out.println("Enter x,y values");
x=s.nextInt();
y=s.nextInt();
x=x+y;
y=x-y;
x=x-y;
System.out.println("x value is: "+(x));
System.out.println("y value is: "+(y));
}
}

Output:
x value is: 20
y value is: 10
Relational Operators
Relational operators are used to check the relation between two variables. These are classified into following types
Operator Example
== x==y
< x<y
> x>y
<= x<=y
>= x>=y
!= x!=y

Relation Of Two Numbers

class Main
{
public static void main(String args[])
{
int x=10,y=20;
boolean z;
z=x>y;
System.out.println("z value is: "+(z));
}
}

Output:
z value is: false
Relation Of Two Numbers Using Commandline Arguments

class Main
{
public static void main(String args[])
{
int x,y;
boolean z;
x=Integer.parseInt(args[0]);
y=Integer.parseInt(args[1]);
z=x>y;
System.out.println("z value is: "+(z));
}
}

Output:
z value is: false
Relation Of Two Numbers Using Scanner

import java.util.*;
class Main
{
public static void main(String args[])
{
int x,y;
boolean z;
Scanner s=new Scanner(System.in);
System.out.println("Enter x,y values");
x=s.nextInt();
y=s.nextInt();
z=x>y;
System.out.println("z value is: "+(z));
}
}

Output:
z value is: false
Logical Operators
Logical Operators are used to combine the morethan one condition. These are classified into following types.
Operator Example
&& x>y && x>z
|| x>y || x>z
! !(x>y && x>z)

!(x>y || x>z)

!(x>y) && !(x>z)

!(x>y) || !(x>z)
Logical Operators

class Main
{
public static void main(String args[])
{
int x=10,y=20,z=30
boolean p;
p=x>y && x>z;
System.out.println("p value is: "+(p));
}
}

Output:
p value is: false
Logical Operators Using Commandline Arguments

class Main
{
public static void main(String args[])
{
int x,y,z;
boolean p;
x=Integer.parseInt(args[0]);
y=Integer.parseInt(args[1]);
z=Integer.parseInt(args[2]);
p=x>y && x>z;
System.out.println("p value is: "+(p));
}
}

Output:
p value is: false
Logical Operators Using Scanner

import java.util.*;
class Main
{
public static void main(String args[])
{
int x,y,z;
boolean p;
Scanner s=new Scanner(System.in);
System.out.println("Enter x,y,z values");
x=s.nextInt();
y=s.nextInt();
z=s.nextInt();
p=x>y && x>z;
System.out.println("p value is: "+(p));
}
}

Output:
p value is: false
Bitwise Operators
Bitwise Operators are used to perform any operaion on binary(0 and 1) values.These are classified into following types
Operator Operation Example
& AND x&y
| OR x|y
^ CAP x ^ y
~ NAGETION ~x
<< LEFT SHIFT x<<y
>> RIGHT SHIFT x>>y

Bitwise and(&) Operator

class Main
{
public static void main(String args[])
{
int x=10,y=15,z
z=x & y;
System.out.println("z value is: "+(z));
}
}

Output:
z value is: 10
Bitwise and(&) Operator Using Commandline Arguments

class Main
{
public static void main(String args[])
{
int x,y,z;
x=Integer.parseInt(args[0]);
y=Integer.parseInt(args[1]);
z=x & y;
System.out.println("z value is: "+(z));
}
}

Output:
z value is: 10
Bitwise and(&) Operators Using Scanner

import java.util.*;
class Main
{
public static void main(String args[])
{
int x,y,z;
Scanner s=new Scanner(System.in);
System.out.println("Enter x,y values");
x=s.nextInt();
y=s.nextInt();
z=x & y;
System.out.println("z value is: "+(z));
}
}

Output:
z value is: 10

Bitwise or ( | ) Operators

class Main
{
public static void main(String args[])
{
int x=10,y=15,z;
z=x | y;
System.out.println("z value is: "+(z));
}
}

Output:
z value is: 15

Bitwise CAP ( ^ ) Operators

class Main
{
public static void main(String args[])
{
int x=10,y=15,z;
z=x ^ y;
System.out.println("z value is: "+(z));
}
}

Output:
z value is: 5

Bitwise negation ( ~ ) Operator

class Main
{
public static void main(String args[])
{
int x=10;
z= ~x;
System.out.println("z value is: "+(z));
}
}

Output:
z value is: -11

Bitwise left shift( << ) Operator

class Main
{
public static void main(String args[])
{
int x=10;
z=x << 2;
System.out.println("z value is: "+(z));
}
}

Output:
z value is: 40

Bitwise Right shift( >> ) Operator

class Main
{
public static void main(String args[])
{
int x=10;
z=x >> 2;
System.out.println("z value is: "+(z));
}
}

Output:
z value is: 2
Conditional or Ternary Operator (?:)
Ternary operator is used to perform any operation on three operands
Syntax :
Expression1 ? Expression2 : Expression3;

Here
Expression1 is a condition
Expression2 is a operation
Expression3 is a operation

In the above syntax
if condition is true ,The statements of Expression2 is execute.
if condition is false ,The statements of Expression3 is execute
Odd or Even Number Using Ternary Operator

class Main
{
public static void main(String args[])
{
int x=10;
String s;
s= x%2==0 ? "EvenNumber" : "OddNumber";
System.out.println(s);
}
}

Output:
EvenNumber
Odd or Even Number Using Ternary Operator and Commandline Arguments

class Main
{
public static void main(String args[])
{
int x;
x=Integer.parseInt(args[0]);
String s;
s= x%2==0 ? "EvenNumber" : "OddNumber";
System.out.println(s);
}
}

Output:
EvenNumber
Odd or Even Number Using Ternary Operator and Scanner

import java.util.*;
class Main
{
public static void main(String args[])
{
int x;
Scanner s1=new Scanner(System.in);
System.out.println("Enter x value");
x=s1.nextInt();
String s;
s= x%2==0 ? "EvenNumber" : "OddNumber";
System.out.println(s);
}
}

Output:
EvenNumber
MAX Number Using Ternary Operator
Max Number Using Ternary Operator

class Main
{
public static void main(String args[])
{
int x=10,y=20;
String s;
s= x>y ? "x is Max" : "y is Max";
System.out.println(s);
}
}

Output:
y is Max
Max Number Using Ternary Operator and Commandline Arguments

class Main
{
public static void main(String args[])
{
int x,y;
x=Integer.parseInt(args[0]);
y=Integer.parseInt(args[1]);
String s;
s= x>y ? "x is Max" : "y is Max";
System.out.println(s);
}
}

Output:
Enter x ,y value
10
20
y is Max
Max Number Using Ternary Operator and Scanner

import java.util.*;
class Main
{
public static void main(String args[])
{
int x,y;
Scanner s1=new Scanner(System.in);
System.out.println("Enter x ,y value");
x=s1.nextInt();
y=s1.nextInt();
String s;
s= x>y ? "x is Max" : "y is Max";
System.out.println(s);
}
}

Output:
Enter x ,y value
10
20
y is Max
MAX Number Amoung 3 Numbers Using Ternary Operator
MAX Number Amoung 3 Numbers

class Main
{
public static void main(String args[])
{
int x=10,y=20,z=30
String s;
s=(x > y) ?
(x > z ? "x is Max" : "z is Max") :(y > z ? "y is Max" : "z is Max");
System.out.println(s);
}
}

Output:
z is Max
MAX Number Amoung 3 Numbers Using Ternary Operator and Commandline Arguments

class Main
{
public static void main(String args[])
{
int x,y,z;
x=Integer.parseInt(args[0]);
y=Integer.parseInt(args[1]);
z=Integer.parseInt(args[2]);
String s;
s=(x > y) ?
(x > z ? "x is Max" : "z is Max") :(y > z ? "y is Max" : "z is Max");
System.out.println(s);
}
}

Output:
Enter x ,y value
10
20
z is Max
MAX Number Amoung 3 Numbers Using Ternary Operator and Scanner

import java.util.*;
class Main
{
public static void main(String args[])
{
int x,y,z;
Scanner s1=new Scanner(System.in);
System.out.println("Enter x ,y ,z value");
x=s1.nextInt();
y=s1.nextInt();
z=s1.nextInt();
String s;
s=(x > y) ?
(x > z ? "x is Max" : "z is Max") :(y > z ? "y is Max" : "z is Max");
System.out.println(s);
}
}

Output:
Enter x ,y value
10
20
30
z is Max
Simple Interest Program

class Main
{
public static void main(String args[])
{
int a=2000,d=2,i=2,si;
si= (a*d*i)/100;
System.out.println("Simple Interest is: "+(si));
}
}

Output:
Simple Interest is: 80
Simple Interest Program Using Commandline Arguments

class Main
{
public static void main(String args[])
{
int a,d,i,si;
a=Integer.parseInt(args[0]);
d=Integer.parseInt(args[1]);
i=Integer.parseInt(args[2]);
si= (a*d*i)/100;
System.out.println("Simple Interest is: "+(si));
}
}

Output:
Simple Interest is: 80
Simple Interest Program Using Scanner

import java.util.*;
class Main
{
public static void main(String args[])
{
int a,d,i,si;
Scanner s=new Scanner(System.in);
System.out.println("Enter Amount:");
a=s.nextInt();
System.out.println("Enter Duration:");
d=s.nextInt();
System.out.println("Enter Interest:");
i=s.nextInt();
si= (a*d*i)/100;
System.out.println("Simple Interest is: "+(si));
}
}

Output:
Enter Amount:
2000
Enter Duration:
2
Enter Interest:
2
Simple Interest is: 80