Operators in java

 Operators:

  • Operators are symbols and used to perform some mathematics operations.
  • There are 6 kinds of operators,
1.Arithmetic operators: 
  • This is normal mathematic operations such as,
Example program for the arithmetic operator:

package exampleprogram;

import java.util.Scanner;

public class Operator {

    public static void main(String[] args){

       Scanner input = new Scanner(System.in);

       int a,b;

       System.out.println("\t\t  Arithmetic Operator.... ");

       System.out.println("Enter two value  : ");

       a = input.nextInt();

       b = input.nextInt();

       System.out.println("Add value is : "+(a+b));

       System.out.println("Sub value is : "+(a-b));

       System.out.println("Mul value is : "+(a*b));

       System.out.println("Div value is : "+(a/b));

       System.out.println("Mod value is : "+(a%b));

       System.out.println("Post increament of a value is : "+(a++));

       System.out.println("Pre increament of a value is : "+(++a));

       System.out.println("Post decreament of b value is : "+(b--));

       System.out.println("Pre increament of a value is : "+(--b));

    }

}

Output:
2. Relational Operators:
  • It is used to compare two or more data.
Note:
  • These logical operators mostly used in conditions.
3. Logical Operators:
  • It is used to combine simple relational statements into more complex expressions. 
4. Assignment Operators:
  • These operators are used to getting the result in the same variable(input/output).
Example program for the assignment operator:

package exampleprogram;
import java.util.Scanner;
public class Operator {
    public static void main(String[] args){
       Scanner input = new Scanner(System.in);
       int a,b;
       System.out.println("\t\t  Assignment Operator.... ");
       System.out.println("\nEnter a value  : ");
       a = input.nextInt();
       a+=10;
       System.out.println("Now a value is : "+a);
    } 
    
}

Output:
5. Conditional Operators (? :)
  • It checks itself the condition and executes the statement depending on the condition.
Syntax:
Condition?exp1:exp2;
Example program for the conditional operator:

package exampleprogram;

import java.util.Scanner;

public class Operator {

    public static void main(String[] args){

       Scanner input = new Scanner(System.in);

       int a,b;

       System.out.println("\t\t  Conditional Operator.... ");

       System.out.println("\nEnter a and b value  : ");

       a = input.nextInt();

       b = input.nextInt();

       int big=a>b?a:b;

       System.out.println("Biggest value is : "+big);

    }

}

Output:

6. Bitwise Operators:
  • It is used to manipulate the data at a bit level and it operates on integer only.
                                                                                  Binary table

Bitwise AND(&):
  • The truth table for ‘ & ‘is shown below(It means True=1 and false=0.)
a=7 of binary code:0111
b=8 of the binary code is:1000
The result of the a&a value is 0000(i.e 0).




Example program for the bitwise AND operator:

package exampleprogram;

import java.util.Scanner;

public class Operator {

    public static void main(String[] args){

       Scanner input = new Scanner(System.in);

       int a,b;

       System.out.println("\t\t  Bitwise AND Operator.... ");

       System.out.println("\nEnter a and b value  : ");

       a = input.nextInt();

       b = input.nextInt();

       int c=a&b;

       System.out.println("Bitwise AND of a&b value is : "+c);

    }

}

Output:
Bitwise OR(|):
  • The truth table for ‘ | ‘is shown below
a=7 of binary code:0111
b=8 of the binary code is:1000
The result of a|b value is 1111(i.e 15).




Example program for the bitwise OR operator:

package exampleprogram;

import java.util.Scanner;

public class Operator {

    public static void main(String[] args){

       Scanner input = new Scanner(System.in);

       int a,b;

       System.out.println("\t\t  Bitwise OR Operator.... ");

       System.out.println("\nEnter a and b value  : ");

       a = input.nextInt();

       b = input.nextInt();

       int c=a|b;

       System.out.println("Bitwise OR of a|b value is : "+c);

    }

}

Output:
Bitwise Exclusive OR(^):
  • The truth table for ‘ ^ ‘is shown below
a=13 of binary code:1101
b=8 of the binary code is:1000
The result of a^b value is 0101(i.e 5).



Example program for the bitwise Exclusive OR operator:

package exampleprogram;

import java.util.Scanner;

public class Operator {

    public static void main(String[] args){

       Scanner input = new Scanner(System.in);

       int a,b;

       System.out.println("\t\t  Bitwise Exclusive OR Operator.... ");

       System.out.println("\nEnter a and b value  : ");

       a = input.nextInt();

       b = input.nextInt();

       int c=a^b;

       System.out.println("Bitwise Exclusive OR of a^b value is : "+c);

    }

}

Output:
Bitwise Shift left (<<):
  • The bitwise shift left operator is used to move the bits from right to left in storage.
  • Each box called bits and a=6 and a<<1 then a=12.
Explanation:







Result: a of left shit value is 12.


Example program for the bitwise shift left operator:

package exampleprogram;

import java.util.Scanner;

public class Operator {

    public static void main(String[] args){

       Scanner input = new Scanner(System.in);

       int a;

       System.out.println("\t\t  Bitwise left shit Operator.... ");

       System.out.println("\nEnter a value  : ");

       a = input.nextInt();

       int c=a<<1;

       System.out.println("Bitwise left shit of a value is : "+c);

    }

}

Output:
Bitwise Shift right(>>):
  • The shit right is similar to the left shit.
  • This operator is used to forward the bits from left to right in storage.
  • For example, If a=6 and a>>1 then its value is 3.
Explanation:


Result: a of right shit value is 3.



Example program for the bitwise shift right operator:

package exampleprogram;

import java.util.Scanner;

public class Operator {

    public static void main(String[] args){

       Scanner input = new Scanner(System.in);

       int a;

       System.out.println("\t\t  Bitwise right shit Operator.... ");

       System.out.println("\nEnter a value  : ");

       a = input.nextInt();

       int c=a>>1;

       System.out.println("Bitwise right shit of a value is : "+c);

    }

}

Output:
Bitwise One's complement(~):
  • The truth table for ‘ ~ ‘is shown below

If a=6(binary code is 0110),
 and ~a(0111) value is 7. 
using a negative signature for one's complement(~a=-7).


Example program for the bitwise one's complement operator:

package exampleprogram;

import java.util.Scanner;

public class Operator {

    public static void main(String[] args){

       Scanner input = new Scanner(System.in);

       int a;

       System.out.println("\t\t  Bitwise one\'s complement Operator.... ");

       System.out.println("\nEnter a value  : ");

       a = input.nextInt();

       int c=~a;

       System.out.println("Bitwise one\'s complement of a value is : "+c);

    }

}

Output:
Share:

No comments:

Post a Comment

Popular Posts

Search This Blog

Powered by Blogger.

Blog Archive

Service Support

Need our help to Learn or Post New Concepts Contact me

Blog Archive

Recent Posts

Service Support

Need our help to Learn or Post New Concepts Contact me