Arithmetic, Unary, Equality, Relational, Conditional, Bitwise, Bit Shift, Assignment, instanceof operators in detail with programs in java


Contents of page :
  • 1) The Arithmetic Operators >
    • Program 1 to demonstrate Arithmetic operators
  • 2) The Unary Operators >

    • Program 2 to demonstrate Unary operators
  • 3) Equality, Relational, and Conditional Operators >

    • Program 3.1 to demonstrate equality operators
    • Program 3.2 to demonstrate relational operators
    • Program 3.3 to demonstrate conditional operators
  • 4) The Bitwise & Bit Shift Operators >

    • Program 4.1 to demonstrate Bitwise operators
    • Program 4.2 to demonstrate Bitshift operators
  • 5) Assignment Operators >

    • Program 5 to demonstrate Assignment operators
  • 6) instanceof Operator  (used for Type Comparison)

    • Program 6 to demonstrate instanceof operator


1) The Arithmetic Operators >

Java arithmetic operators work same as they do in mathematics.
Operator
Description
+
Addition
(can be used for String concatenation also)
-
Subtraction
*
Multiplication
/
Division
%
Modulus/ Remainder


Program 1 to demonstrate Arithmetic operators
/** JavaMadeSoEasy.com */
public class ArithmeticOperatorTest {
   public static void main(String[] args) {
          int x=4;
          int y=2;
          int result=x+y;
          System.out.println("+   "+ result); //6
          result=x-y;
          System.out.println("-   "+ result); //2
          result=x*y;
          System.out.println("*   "+ result); //8
          result=x/y;
          System.out.println("/   "+ result); //2
          result=x%y;
          System.out.println("%   "+ result); //0
         
   }
}
/*OUTPUT
+   6
-   2
*   8
/   2
%   0
*/


2) The Unary Operators >

The unary operators require only one operand; they perform various operations such as incrementing/decrementing a value by one, negating an expression, or inverting the value of a boolean.
Operator
Description
+
Unary plus operator;
indicates positive value
(it’s optional, if not used numbers are positive)
-
Unary minus operator;
indicates negative value
++
Increment operator;
increments value by 1
--
Decrement operator;
decrements value by 1
!
Logical complement operator;
inverts boolean value


Program 2 to demonstrate Unary operators
/** JavaMadeSoEasy.com */
public class UnaryOperatorTest {
   public static void main(String[] args) {
          int result=+1;
          System.out.println("+   "+ result); //1
          result=-1;
          System.out.println("-  "+ result); //-1
         
          result=3;
          System.out.println("--------Now, result="+result);
          result++;
          System.out.println("++  "+ result); //4
          result--;
          System.out.println("--  "+ result); //3
          boolean boo=true;
          System.out.println("!   "+ !boo); //false
         
   }
}
/*OUTPUT
+   1
-  -1
Now, result=3
++  4
--  3
!   false
*/



3) Equality, Relational, and Conditional Operators >


Equality Operators

==
equal to
!=     
not equal to


Relational Operators

>      
greater than
>=     
greater than or equal to


<      
less than
<=    
less than or equal to


Conditional Operators

&&
Conditional AND

operator1
operator2
Result
true
true
true
true
false
false
false
true
false
false
false
false
||
Conditional OR

operator1
operator2
Result
true
true
true
true
false
true
false
true
true
false
false
false

Ternary operator

?:
ternary operator



Program 3.1 to demonstrate equality operators
/** JavaMadeSoEasy.com */
public class EqualityOperatorTest {
   public static void main(String[] args) {
          int x=4;
          int y=2;
         
          System.out.println(x==y); //false    
          System.out.println(x!=y); //true
   }
}
/*OUTPUT
false
true
*/



Program 3.2 to demonstrate relational operators
/** JavaMadeSoEasy.com */
public class RelationalOperatorTest {
   public static void main(String[] args) {
          int x=4;
          int y=2;
          System.out.println(x>y); //true      
          System.out.println(x>=y); //true     
         
          System.out.println(x<y); //false
          System.out.println(x<=y); //false
         
          System.out.println(x>=4); //true
          System.out.println(y<=2); //true
         
   }
}
/*OUTPUT
true
true
false
false
true
true
*/



Program 3.3 to demonstrate conditional operators
/** JavaMadeSoEasy.com */
public class ConditionalOperatorTest {
   public static void main(String[] args) {
          int x=4;
          int y=2;
          System.out.println("---------  && -----------");
         
          System.out.println((x==4) && (y==2)); //true -->> (x==4) returns true, (y==2) also returns true,
                                                                     //So,  (true && true) returns true
          System.out.println((x==7) && (y==2)); //false -->> (x==7) returns false, so (y==2) is not evaluated.
                                                                     //So,  (false && (true/false)) returns false
          System.out.println((x==4) && (y==9));  //false -->> (x==4) returns true, but (y==2) returns false.
                                                                     //So,  (true && false) returns false
          System.out.println("---------  || -----------");
         
          System.out.println((x==4) || (y==8));  //true -->> (x==4) returns true, so (y==8) is not evaluated.
                                                                            //So,  (true || (true/false)) returns true
          System.out.println((x==7) || (y==2));   //true -->> (x==7) returns false, (y==2) returns true
                                                                            //So,  (false || (true/false)) returns true
          System.out.println((x==9) || (y==8)); //false  -->> (x==9) returns false, (y==2) also returns false
                                                                            //So,  (false || false) returns false
   }
}
/*OUTPUT
---------  && -----------
true
false
false
---------  || -----------
true
true
false
*/



Program 3.4 to demonstrate ternary operator >
/** JavaMadeSoEasy.com */
public class TernaryConditionalOperatorTest {
   public static void main(String[] args) {
          int x=3;
          System.out.println( (x >4) ?   "x is > 4"   :   "x is < 4" );
          System.out.println( (x==3) ?   "x == 3" :   "x != 3" );
         
   }
}
/*OUTPUT
x is < 4
x == 3
*/



4) The Bitwise & Bit Shift Operators >


Bitwise operators can be applied on byte, short, char, int and long.
Bitwise operator performs operations on bits.




Bitwise Operators
&
bitwise AND operator

operator1
operator2
bitwise AND operator
Result
0
0
0
0
1
0
1
0
0
1
1
1

^
bitwise EXCLUSIVE OR operator (XOR)

operator1
operator2
bitwise EXCLUSIVE OR
Result
0
0
0
0
1
1
1
0
1
1
1
0

|
bitwise INCLUSIVE OR operator

operator1
operator2
bitwise INCLUSIVE OR
Result
0
0
0
0
1
1
1
0
1
1
1
1

~
unary bitwise complement operator

Inverts the bits

operator
unary bitwise complement operator
Result
0
1
1
0




Bitshift Operators
<<
bitwise Left Shift Operator
Moves specified bits to left.
>>
bitwise Right Shift Operator
Moves specified bits to right.
>>>
unsigned Right Shift Operator
shifts a zero into the leftmost position, while the leftmost position after ">>" depends on sign extension



Program 4.1 to demonstrate Bitwise operators
/** JavaMadeSoEasy.com */
public class BitWiseOperatorTest {
   public static void main(String[] args) {
         
          int x=38; //0010 0110
          int y=58; //0011 1010
          System.out.println(x+" - "+Integer.toBinaryString(x));
          System.out.println(y+" - "+Integer.toBinaryString(y));
          int z=x&y;
          System.out.println("\n & operator");
          System.out.println(z+" - "+Integer.toBinaryString(z));
          z=x^y;
          System.out.println("\n ^ operator");
          System.out.println(z+" -  "+Integer.toBinaryString(z));
          z=x|y;
          System.out.println("\n | operator");
          System.out.println(z+" - "+Integer.toBinaryString(z));
         
         
   }
}
/*OUTPUT
38 - 100110
58 - 111010
& operator
34 - 100010
^ operator
28 -  11100
| operator
62 - 111110
*/



Program 4.2 to demonstrate Bitshift operators
/** JavaMadeSoEasy.com */
public class BitShiftOperatorTest {
   public static void main(String[] args) {
          //128,64,32,16,   8,4,2,1
          int x=38; //0010 0110
          System.out.println(x+" - "+Integer.toBinaryString(x));
          int z=x>>2;
          System.out.println("\n >> operator");
          System.out.println(z+" -  "+Integer.toBinaryString(z));
          z=x<<2;
          System.out.println("\n << operator");
          System.out.println(z+" - "+Integer.toBinaryString(z));
          x=38;
          z=x>>>2;
          System.out.println("\n >>> operator");
          System.out.println(z+" - "+Integer.toBinaryString(z));
         
         
   }
}
/*OUTPUT
38 - 100110
>> operator
9 -  1001
<< operator
152 - 10011000
>>> operator
9 - 1001
*/





5) Assignment Operators >

Assignment operators can be used with all arithmetic, bitwise and bit shift operators.

Operator
Description
=
assignment operator

x = y  assigns y to x.

+=
Add AND assignment operator

x -= y  is equivalent to (x = x-y)
-=
Subtract AND assignment operator

x -= y  is equivalent to (x = x-y)
*=
Multiply AND assignment operator

x *= y  is equivalent to (x = x*y)
/=
Divide AND assignment operator

x /= y  is equivalent to (x = x/y)



Program 5 to demonstrate Assignment operators
/** JavaMadeSoEasy.com */
public class AssignmentOperatorTest {
   public static void main(String[] args) {
          int x=4;
          int y=2;
          x += y;
          System.out.println("+=   "+ x); //6
         
          x=4; y=2;
         
          x -= y;
          System.out.println("-=   "+ x); //2
         
          x=4; y=2;
         
          x *= y;
          System.out.println("*=   "+ x); //8
         
          x=4; y=2;
         
          x /= y;
          System.out.println("/=   "+ x); //2
         
   }
}
/*OUTPUT
+=   6
-=   2
*=   8
/=   2
*/





6) instanceof Operator  (used for Type Comparison)

  • instanceof operator compares an object to a specified type.
  • instanceof operator test if an object is an instance of a
    • class or
    • subclass or.
    • Any implemented interface.
  • By default all reference variable of all classes/interface are instanceof Java.lang.Object class
  • null is instanceof nothing.




Program 6 to demonstrate instanceof operator
class SuperClass {}
class SubClass extends SuperClass{}
/** JavaMadeSoEasy.com */
public class InstanceofTest {
   public static void main(String[] args) {
       SuperClass sup = new SuperClass();
       SuperClass sub = new SubClass();
       System.out.println("------------------SubClass instanceOf---");
      
       System.out.println("SuperClass instanceof SuperClass - "
        + (sup instanceof SuperClass));
       System.out.println("SuperClass instanceof SubClass - "
        + (sup instanceof SubClass));
       System.out.println("SuperClass instanceof java.lang.Object - "
        + (sup instanceof java.lang.Object));
       System.out.println("\n------------------SubClass instanceOf---");
      
       System.out.println("SubClass instanceof SuperClass - "
        + (sub instanceof SuperClass));
       System.out.println("SubClass instanceof SubClass - "
        + (sub instanceof SubClass));
       System.out.println("SubClass instanceof java.lang.Object - "
        + (sub instanceof java.lang.Object));
   }
}
/*OUTPUT
------------------SubClass instanceOf---
SuperClass instanceof SuperClass - true
SuperClass instanceof SubClass - false
SuperClass instanceof java.lang.Object - true
------------------SuperClass instanceOf---
SubClass instanceof SuperClass - true
SubClass instanceof SubClass - true
SubClass instanceof java.lang.Object - true
*/




RELATED LINKS>

Primitive, Custom/reference Data Types, Integer, Floating-Point, Character and String literal, Escape sequence in java, decimal to hexaDecimal and binary conversion program


Control Flow Statements = Decision making - if, else, switch | Looping - for, Infinite, Enhanced for loop, while, do-while | Branching - break, Labeled break, continue, Labeled continue, return, goTo


Labels: Core Java
eEdit
Must read for you :