Saturday, March 10, 2018

Summary of Operators in Java With Examples

What is Operator in Java:-

An operator, in Java, is a special symbol performing specific operations on one,  or three operands after which returning a result. The operators are labeled and listed in keeping with the priority order. Java operators are normally used to manipulate primitive information types.
Operator in java is a symbol that is used to perform operations. For example: +, -, *, / etc.

There are many types of operators in java which are given below:
  • Arithmetic Operator
  • Relational Operator
  • Bitwise Operator
  • Logical Operator
  • Assignment Operator
  • Unary Operator
  • Special Operator
  • Shift Operator

Arithmetic Operator in Java with Example:-

Arithmetic operators are taking numerical values (both literals or variables) as their operands and return an unmarried numerical value. the usual arithmetic operators are addition (+), subtraction (-), multiplication (*), and division (/)
class my
{  
       public static void main(String args[])
      {  
            int a=10;  
            int b=5;   
           System.out.println(a+b);//15  
           System.out.println(a-b);//5  
           System.out.println(a*b);//50  
           System.out.println(a/b);//2  
           System.out.println(a%b);//0  
      }
}  

Relation Operator in Java With Example:

In computer technology, a relational operator is a programming language construct or operator that exams or defines a few types of relation between two entities. these consist of numerical equality (e.g., 5 = five) and inequalities (e.g., four ≥ three).

public class Test 
{
      public static void main(String args[]) 
  {
      int a = 10;
      int b = 20;
      System.out.println("a == b = " + (a == b) );
      System.out.println("a != b = " + (a != b) );
      System.out.println("a > b = " + (a > b) );
      System.out.println("a < b = " + (a < b) );
      System.out.println("b >= a = " + (b >= a) );
      System.out.println("b <= a = " + (b <= a) );
   }
}
Output
a == b = false
a != b = true
a > b = false
a < b = true
b >= a = true
b <= a = false

Bitwise operator in Java With Example:-

Bitwise operators work on bits and perform a bit.In digital computer programming, a bitwise operation operates on one or greater bit styles or binary numerals at the level of their individual bits. it's for a quick, easy action without delay supported by the processor, and is used to govern values for comparisons and calculations.
public class Test {
public static void main(String args[]) {
      int a = 60; /* 60 = 0011 1100 */
      int b = 13; /* 13 = 0000 1101 */
      int c = 0;
      c = a & b;        /* 12 = 0000 1100 */
      System.out.println("a & b = " + c );
      c = a | b;        /* 61 = 0011 1101 */
      System.out.println("a | b = " + c );
      c = a ^ b;        /* 49 = 0011 0001 */
      System.out.println("a ^ b = " + c );
      c = ~a;           /*-61 = 1100 0011 */
      System.out.println("~a = " + c );
      c = a << 2;       /* 240 = 1111 0000 */
      System.out.println("a << 2 = " + c );
      c = a >> 2;       /* 15 = 1111 */
      System.out.println("a >> 2  = " + c );
      c = a >>> 2;      /* 15 = 0000 1111 */
      System.out.println("a >>> 2 = " + c );
   } 
}
Output:
a & b = 12
a | b = 61
a ^ b = 49
~a = -61
a << 2 = 240
a >> 15
a >>> 15

Logical Operator in Java With Example:-

Assume boolean variables A holds true and variable B holds false then: The logical OR operator ( || ) returns the boolean price actual if either or each operand is proper and returns false in any other case. The operands are implicitly transformed to kind bool previous to assessment, and the end result is of type bool.

public class my 
{
      public static void main(String args[])
     {
          boolean a = true;
          boolean b = false;
          System.out.println("a && b = " + (a&&b));
          System.out.println("a || b = " + (a||b) );
          System.out.println("!(a && b) = " + !(a && b));
   }
}
Output:
a && b = false
a ||  b = true
!(a && b) = true

Assignment Operator in Java With Example:-

The fundamental challenge operator is equal ( = ), which assigns the value of its right operand to its left operand. that is, x = y assigns the fee of y to x . the opposite undertaking operators are normally shorthand for popular operations, as shown within the following definitions and examples
public class Test {

   public static void main(String args[]) {
      int a = 10;
      int b = 20;
      int c = 0;

      c = a + b;
      System.out.println("c = a + b = " + c );
      c += a ;
      System.out.println("c += a  = " + c );
      c -= a ;
      System.out.println("c -= a = " + c );
      c *= a ;
      System.out.println("c *= a = " + c );
      a = 10;
      c = 15;
      c /= a ;
      System.out.println("c /= a = " + c );
      a = 10;
      c = 15;
      c %= a ;
      System.out.println("c %= a  = " + c );
      c <<= 2 ;
      System.out.println("c <<= 2 = " + c );
      c >>= 2 ;
      System.out.println("c >>= 2 = " + c );
      c >>= 2 ;
      System.out.println("c >>= 2 = " + c );
      c &= a ;
      System.out.println("c &= a  = " + c );
      c ^= a ;
      System.out.println("c ^= a   = " + c );
      c |= a ;
      System.out.println("c |= a   = " + c );
   }
}
Output:
c = a + b = 30
c += a  = 40
c -= a = 30
c *= a = 300
c /= a = 1
c %= a  = 5
c <<= 2 = 20
c >>= 2 = 5
c >>= 2 = 1
c &= a  = 0
c ^= a   = 10
c |= a   = 10

Unary Operator in Java With Example:-

The unary administrators require just a single operand; they perform different operations, for example, increasing/decrementing an incentive by one, discrediting an articulation, or transforming the estimation of a boolean.
class demo
{  
public static void main(String args[])
{  
int x=10;  
System.out.println(x++);//10 (11)  
System.out.println(++x);//12  
System.out.println(x--);//12 (11)  
System.out.println(--x);//10  
}       
}  
Output:
10
12
12
10

Java Shift Operator in Java With Example:-

Bitwise and Bit Shift Operators. The Java programming dialect likewise gives administrators that perform bitwise and bit move operations on essential sorts. ... The bit design is given by the left-hand operand, and the quantity positions to move by the right-hand operand.
  • Left Shift Operator Example

class my
{  
public static void main(String args[])
          {  
System.out.println(10<<2);//10*2^2=10*4=40  
System.out.println(10<<3);//10*2^3=10*8=80  
System.out.println(20<<2);//20*2^2=20*4=80  
System.out.println(15<<4);//15*2^4=15*16=240  
          }
}  
Output:
40
80
80
240
  • Right Shift Operator Example
class my
{
public static void main(String args[])
{
System.out.println(10>>2);//10/2^2=10/4=2  
System.out.println(20>>2);//20/2^2=20/4=5
System.out.println(20>>3);//20/2^3=20/8=2 
}
}  
Output
2
5
2


No comments:

Post a Comment

Top 200+ Core Java Interview Questions And Answers For 2019

Most Popular Java Interview Questions Given below is a comprehensive list of most important and commonly asked basic and advanced Java p...