There are 4 Operators in Java Programming
- Arithematic operators
- Logical operators
- Assignment operators
- Relational operators
- Incremental, Decremental operators
Arithematic Operators
| + | Addition |
| – | Subtraction |
| * | Multiplication |
| / | Division |
| % | Modulus |
Example 1
public class Arithematic
{
public static void main(String[] args)
{
int x;
int y;
x =50;
y=6;
System.out.println(x + y);
System.out.println(x - y);
System.out.println(x / y);
System.out.println(x % y);
}
}
Output
56
44
8
2
Example 2
public class Arithematic2 {
public static void main(String[] args)
{
int x;
int y;
x =20;
y = 10;
System.out.println("x+y= "+ (x+y));
System.out.println("x-y= "+ (x-y));
System.out.println("x*y= "+ (x*y));
System.out.println("x/y= "+ (x/y));
System.out.println("x%y= "+ (x%y));
}Output
x+y= 30
x-y= 10
x*y= 200
x/y= 2
x%y= 0
Example 3
public class Arithematic3
{
public static void main(String[] args)
{
int x=20;
int y=10;
int z=30;
System.out.println(x+y);
System.out.println(x+y+z);
System.out.println(x+z);
System.out.println(z-x);
System.out.println(x*z);
System.out.println(z/x);
}
}Output
30
60
50
10
600
1
Logical Operators
| && | And Operator |
| || | or Operator |
| ! | Not opertor |
Example 1
public class Logical
{
public static void main(String[] args)
{
int x=20;
int y=10;
System.out.println(x>2&&y>2);
System.out.println(x>2&&y>30);
System.out.println(x>15&&y>10);
System.out.println(x>2&&y>2);
System.out.println(x>2&&y>30);
System.out.println(x>2||y>2);
System.out.println(x>2||y>30);
System.out.println(x>15||y>10);
System.out.println(!(y>x));
}
}Output
true
false
false
true
false
true
true
true
true
Example 2
public class Logical2
{
public static void main(String[] args)
{
int x=100;
int y=50;
System.out.println(x>2&&y>2);
System.out.println(x>2&&y>30);
System.out.println(x>15&&y>10);
System.out.println(x>2||y>2);
System.out.println(x>2||y>2);
System.out.println(x>2||y>30);
System.out.println(x>15||y>10);
System.out.println(!(y>x));
System.out.println(!(x>y));
}
}Output
true
true
true
true
true
true
true
true
false
Assignment Operators
| = | Assignment |
| += | Assignment after addition |
| -= | Assignment after substraction |
| *= | Assignment after multiplication |
| /= | Assignment after division |
| %= | Assignment after mod |
Increment,Decrement Operators
| ++ | Increment Operator |
| — | Decrement Operator |
Increment Operator
Example 1
public class Increment
{
public static void main(String[] args)
{
int x=5;
int y=10;
System.out.println("x= " +x);
System.out.println("y= " +y);
++x;
++y;
System.out.println("x= " +x);
System.out.println("y= " +y);
}
}Output
x= 5
y= 10
x= 6
y= 11
public class Increment1
{
public static void main(String[] args)
{
int x=5;
int y=10;
System.out.println("x= " +x + " y= " + y);
++x;
System.out.println("x= " +x + " y= " + y);
++y;
++y;
System.out.println("x= " +x + " y= " + y);
++x;
++x;
++x;
++y;
System.out.println("x= " +x + " y= " + y);
}
}Output
x= 5 y= 10
x= 6 y= 10
x= 6 y= 12
x= 9 y= 13
public class Increment2
{
public static void main(String[] args)
{
int Age=25;
++Age;
++Age;
++Age;
++Age;
System.out.println("Age=" +Age);
++Age;
++Age;
++Age;
System.out.println("Age=" +Age);
}
}Output
Age=29
Age=32
Decrement Operator
public class Decrement{
public static void main(String[] args) {
int x = 10;
int z = 20;
System.out.println("x= " + x + " z= " +z);
--x;
--z;
System.out.println("x= " + x + " z= " +z);
}
}
Output
x= 10 z= 20
x= 9 z= 19
public class Decrement2{
public static void main(String[] args) {
int x = 10;
int z = 20;
System.out.println("x= " + x + " z= " +z);
--x;
System.out.println("x= " + x + " z= " +z);
--x;
--x;
System.out.println("x= " + x + " z= " +z);
--x;
--x;
--x;
--z;
System.out.println("x= " + x + " z= " +z);
}Output
x= 10 z= 20
x= 9 z= 20
x= 7 z= 20
x= 4 z= 19
Relational Operators
| != | not equlal to |
| — | is equal to |
| > | greater than |
| < | less than |
| >= | greater than or equal to |
| <= | less than or equal to |
public class Relational{
public static void main(String[] args) {
int x = 10;
int z = 20;
System.out.println(x==z);
System.out.println(x!=z);
System.out.println(x>z);
System.out.println(x<z);
System.out.println(x>=z);
System.out.println(x<=z);
}
}Output
false
true
false
true
false
true
public class Relational1{
public static void main(String[] args) {
int x = 10;
int y = 20;
int z = ++x;
int d = y++;
System.out.println("x= "+x);
System.out.println("y= "+y);
System.out.println("z= "+z);
System.out.println("d= "+d);
}
}Output
x= 11
y= 21
z= 11
d= 20
public class Relational2{
public static void main(String[] args) {
int x = 10;
int y = 10;
int z = ++x;
int d = y++;
System.out.println("x= "+x);
System.out.println("y= "+y);
System.out.println("z= "+z);
System.out.println("d= "+d);
x++;
++y;
System.out.println("x+y+z+d=" + (x+y+z+d));
++z;
++d;
System.out.println("x+y+z+d=" + (x+y+z+d));
}
}
Output
x= 11
y= 11
z= 11
d= 10
x+y+z+d=45
x+y+z+d=47
public class Relational3{
public static void main(String[] args) {
int x = 10;
int y = 10;
int z = ++x;
int d = y++;
System.out.println(++y+"," +z+", "+d);
System.out.println(y++ +","+ ++x+", "+ y*d);
System.out.println(x+y+z+d);
}
}Output
12,11, 10
12,12, 130
46
public class Relational4{
public static void main(String[] args) {
int x = 10;
int y = 10;
int z = 20;
int d = 30;
int e = 40;
System.out.println(x==20||y>20);
System.out.println(x>5&&e>50);
System.out.println(x==10&&e==30);
System.out.println(d<30||z>20);
System.out.println(e>10&&d==30&&x<5);
System.out.println(!(x==10||e==30));
System.out.println(!(z<5||y>0&&e<50));
System.out.println(x==10&&e==30);
System.out.println(!(x==10||e==30));
System.out.println(z<5||y>0&&e<50);
System.out.println(!(x<y));
System.out.println(!(x<y&&e==40));
System.out.println(!(e>10&&d==30&&x<5));
System.out.println(!(x==20||y>20));
System.out.println(!(x==5&&e>50));
System.out.println(!(d==30)&&(y>1));
System.out.println((d==30)&&!(y>1));
}
}Output
false
false
false
false
false
false
false
false
false
true
true
true
true
true
true
false
false
Operator Precedence
public class Opertors {
public static void main(String[] args) {
int x =10;
if(x>2)
System.out.println("x>2");
}
}Output
x>2
public class Opertors1 {
public static void main(String[] args) {
int x =100;
if(x>250)
System.out.println("x>250");
else
System.out.println("x>250");
}
}Output
x>250
public class Opertors2 {
public static void main(String[] args) {
int marks = 70;
char x;
if(marks<40)
x='F';
else
if(marks<50)
{
x ='C';
}
else
if(marks<75)
x='B';
else
x ='A';
System.out.println("Your Grade is " +x);
}
}Output
Your Grade is B