next


Java Online Compiler

Java Binary Operators

Binary Operator:

If any operation perform on two operands is known as binary operators.
In java language these opeatrs are classified into following types.
  1. Arithmetic
  2. Assignment
  3. Relational
  4. Logical
  5. Bitwise
Arithmetic Operators
These are the operands can be used to perform simple mathametical operation. Which are classified into following types.
Operator Operation Example
+ Addtion a+b
- Substraction a-b
* Multiplication a*b
/ Division a/b
% Modular Division a%b

Tap for view code

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

CommandLine Arguments
CommandLine Arguments are used to read the values from the keybord
For More Information view Here

class Main
{
 public static void main(String args[])
 {
  int x,y;
  System.out.println("Before converting "+(args[0]+args[1]));
  x=Integer.parseInt(args[0]);
  y=Integer.parseInt(args[1]);
  System.out.println("Addition values is "+(x+y));
 }
}
OUTPUT: java Addition 12 12 Before converting 1212 Addition values is 24

Scanner Class
Scanner class is used to read the values from the keybord
For More Information view Here

class Main
{
 public static void main(String args[])
 {
  int x,y;
  System.out.println("Before converting "+(args[0]+args[1]));
  x=Integer.parseInt(args[0]);
  y=Integer.parseInt(args[1]);
  System.out.println("Addition values is "+(x+y));
 }
}
OUTPUT: java Addition 12 12 Before converting 1212 Addition values is 24

Assignment Operators:
Swapping Of Two Numbers
Assignment operators are used to assign the values to the variable or assign the variable to the variable.Tese are classified into following types.
Operator Example Same as
= a=b a=b
+= a+=b a=a+b
=- a-=b a=a-b
*= a*=b a=a*b
/= a/=b a=a/b
%= a%=b a=a%b
import java.util.*;

class Main
{
 public static void main(String args[])
 {
  int x,y,temp;
  Scanner s=new Scanner(System.in);
  System.out.println("Enter x,y values");
  x=s.nextInt();
  y=s.nextInt();
  System.out.println("Before Swapping \n x Value Is "+x+"\n y value is"+y);
  temp=x;
  x=y;
  y=temp;
  System.out.println("After Swapping \n x Value Is " +x+"\n y value is"+y);
 }
}

Swapping Of Two Numbers Without Using Third Variable

import java.util.*;                               

class Main
{
 public static void main(String args[])
 {
  int x,y;
  Scanner s=new Scanner(System.in);
  System.out.println("Enter x,y values");
  x=s.nextInt();
  y=s.nextInt();
  System.out.println("Before Swapping \n x Value Is" +x+"\n y value is"+y);
  x=x+y;
  y=x-y;
  x=x-y;
  System.out.println("After Swapping \n x Value Is" +x+"\n y value is"+y);
 }
}

Relational Operators
Relational operators are used to check the relation between two variables. These are classified into following types
Operator Example
== a==b
> a>b
< a<b
<= a<=b
>= a>=b
!= a!=b

import java.util.*;                               

class Main
{
 public static void main(String args[])
 {
  int x,y;
  Scanner s=new Scanner(System.in);
  System.out.println("Enter x,y values");
  x=s.nextInt();
  y=s.nextInt();
  System.out.println("The  X and Y Relation is "+(x>y));
 }
}



Relational Operators With Using Third Variable

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

Logical Operators
Logical Operators are used to combine the morethan one condition. These are classified into following types.
Operator Example
&& a>b && a>c
|| a>b || a>c
! !(a>b && a>c)
!(a>b) && !(a>c)

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 ,z values");
  x=s.nextInt();
  y=s.nextInt();
  z=s.nextInt();
  System.out.println("The  X and Y Relation is "+((x>y)&& (x<z)));
 
}
}

Logiccal Operators With Using Third Variable

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 ,z values");
  x=s.nextInt();
  y=s.nextInt();
  z=s.nextInt();
  
 boolean p=((x>y)&& (x>z));
  System.out.println("The  X and Y Relation is "+p);
 }
}

Bitwise Operators:
Bitwise Operators are used to perform any operaion perform on binary values.These are classified into following typs.
Operator Operation Example
& and a&b
| or a|b
^ cap a^b
~ nagetion ~a
<< left shift a<<b
>> right shift x>>y

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("Bitwise AND Value is "+z);
 }
}

Bitwise OR Operator

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("Bitwise OR Value is "+z);
 }
}

Bitwise ^(CAP) Operator

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("Bitwise CAP Value is "+z);
 }
}

Bitwise Nagetion(~) Operator

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

 
  z=(~x);
  System.out.println("Bitwise Nagetion Value is "+z);
 }
}

Bitwise Left Shift Operator

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

 
  z=(x<<2);
  System.out.println("Bitwise Left Shift Value is "+z);
 }
}

Bitwise Right Shift Operator

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