Register Now

Login

Lost Password

Enter your email to reset your password.

BY Author

Java – Operators

Java Arithmetic operators

Java Programming Language Arithmetic Operators

Example

 class ArithmeticOperatorsDemo 
 {
     public static void main(String args[]) 
     {
         int a = 11;
         int b = 5;
         int ans;
		
         ans = a+b; // addition 
         System.out.println("Addition = "+ans);
		
         ans = a-b; // subtraction 
         System.out.println("Subtraction = "+ans);
		
         ans = a*b; // multiplication 
         System.out.println("Multiplication = "+ans);
		
         ans = a/b; // division 
         System.out.println("Division = "+ans);
		
         ans = a%b; // modulus 
         System.out.println("Modulus = "+ans);
     }
 } 

Output

Addition = 16
Subtraction = 6
Multiplication = 55
Division = 2
Modulus = 1

Increment & Decrement operators

Java Programming Language Increment and Decrement Operators

Example

Java Programming Language Increment and Decrement Operators Example

Example for ++ x

 class PreIncrementDemo 
 {
     public static void main(String args[])
     {
         int x = 5;
         int y;
         System.out.println("Before increment");
         System.out.println("x = "+x);

         y = ++x;
		
         System.out.println("After increment");
         System.out.println("x = "+x);
         System.out.println("y = "+y);
     }
 }

Output

Before increment
x = 5
After increment
x = 6
y = 6

Example for x++

 class PostIncrement 
 {
     public static void main(String args[])
     {
         int x = 5;
         int y;
         System.out.println("Before increment");
         System.out.println("x = "+x);

         y = x++;
		
         System.out.println("After increment");
         System.out.println("x = "+x);
         System.out.println("y = "+y);
     }
 } 

Output

Before increment
x = 5
After increment
x = 6
y = 5

Example for – -x

 class PreDecrement 
 {
     public static void main(String args[])
     {
         int x = 5;
         int y;
         System.out.println("Before decrement");
         System.out.println("x = "+x);

         y = --x;
		
         System.out.println("After decrement");
         System.out.println("x = "+x);
         System.out.println("y = "+y);
     }
 }

Output

Before decrement
x = 5
After decrement
x = 4
y = 4

Example for x- –

 class PostDecrement 
 {
     public static void main(String args[])
     {
         int x = 5;
         int y;
         System.out.println("Before decrement");
         System.out.println("x = "+x);

         y = x--;
		
         System.out.println("After decrement");
         System.out.println("x = "+x);
         System.out.println("y = "+y);
     }
 }

Output

Before decrement
x = 5
After decrement
x = 4
y = 5

Java assignment operators

Java Programming Language Assignment OperatorsJava Programming Language Assignment Operators Example
Example

 class AssignmentOperatorsDemo 
 {
     public static void main(String args[])
     {
         int x = 10;
         int y = 5;
		
         y += x;  // y = y+x
         System.out.println("y = "+y);

         y -= x; // y = y-x
         System.out.println("y = "+y);
		
         y *= x; // y = y*x
         System.out.println("y = "+y);
		
         y /= x; // y = y/x
         System.out.println("y = "+y);
		
         y %= x; // y = y%x
         System.out.println("y = "+y);
     }
 }

Output

y = 15
y = 5
y = 50
y = 5
y = 5

Relation operators
Java Programming Language Relation Operators
Example

public class RelationalOperatorDemo 
{
	public static void main(String args[]) 
	{
		int x=5;
		int y=10;
		
		System.out.println("x == y returns "+(x==y));
		System.out.println("x < y returns "+(x<y)); 
        System.out.println("x > y returns "+(x>y));
		System.out.println("x <= y returns "+(x<=y)); 
        System.out.println("x >= y returns "+(x>=y));
		System.out.println("x != y returns "+(x!=y));
	}
}

Output

x == y returns false
x < y returns true 
x > y returns false
x <= y returns true 
x >= y returns false
x != y returns true

Logical or Boolean operators

Java Programming Language Logic or Boolean Operators

Example

public class Logical 
{
	public static void main(String args[]) 
	{
		int a=10, b=5, c=20;
		
		System.out.println("(a>b) && (b<c) = "+((a>b) && (b<c))); 
        System.out.println("(a>b) && (b>c) = "+((a>b) && (b>c)));
		
		System.out.println("(a>b) || (b>c) = "+((a>b) || (b>c)));
		System.out.println("(a<b) || (b>c) = "+((a<b) || (b>c)));
	}
}

Output

Bitwise operators

These operators operate on a bit to perform bit by bit operation.

Java Programming Language Bitwise Operators

 

Leave a reply