Operator in Java

  • Post author:
Download PDF
Advertisement

Operator in Java

Java introduced a large operator’s environment. Many operators can be divided into the main four groups: bitwise, arithmetic, logical, and relational. Java also supports some additional operators that handle special situations in the programs. Here we discuss all of Java’s operators. If you know some basics about C/C++/C# programming languages then there is no problem understanding the Java operators. All integer types in Java except char are signed integers.

1- Bitwise Operators

Programming Language Java defines some several bitwise operators which can apply to the integer’s data types like, byte, short, int, long, and char. These operators can act upon the individual bits of their operands. The bitwise operators manipulate the bits within an integer type, it’s important to understand that what effects such manipulations. Specifically, it is helpful to know how Java stores integer’s data type values and how they show negative numbers. Java uses two’s complement, which means that the negative numbers inverted ( 1’s bits to 0’s bits and 0’s bits to 1’s bits) all of the bits in a value, then add 1 bit to the result.

The following table summarized the bitwise operators:

Operator Result
~ Bitwise unary NOT
| Bitwise OR
^ Bitwise exclusive OR
& Bitwise AND
>>  Shift right
<<  Shift left
>>>  Shift right zero fill
&= Bitwise AND assignment
|= Bitwise OR assignment
^= Bitwise exclusive OR assignment
>>= Shift right assignment
>>>= Shift right zero-fill assignment
<<= Shift left assignment

 

  • The Bitwise Logical Operators

The bitwise logical operators are ~, &, ^, and |. The bitwise operators are applied to each bit within each operand. The following table shows the bitwise logical operators’ operation. Suppose operands are X and Y.

X Y X|Y X&Y ~X X^Y
0 0 0 0 1 0
0 1 1 0 1 1
1 0 1 0 0 1
1 1 1 1 0 0

 

  • The Bitwise NOT Operator (~)

The bitwise NOT Operator also called the bitwise complement. Bitwise NOT operator “~” inverts all the operands bits. For example, the number 38 has the following bits pattern.

100110

After the inverts process bits will change below the pattern.

011001

  • The Bitwise AND (&)

In bitwise AND operator “&”, results will 1 if both operands are also 1 and results will 0 if one of the operands is 0.

For example:

01100101

&

10101100

————-

00100100

 

  • The Bitwise OR ( | )

In bitwise OR operator “|” results will 0 if both operands are also 0 and results will 1 if one of the operands is 1.

Example:

10010011

|

10101000

————-

10111011

 

  • The Bitwise XOR (^)

In bitwise XOR operator, “^” results will 1 when both operands are different and the result will 0 when both operands are the same 0 or 1.

For example:

10010011

^

10100100

—————-

00110111

 

  • The Left Shift (<<)

The left shift operator “<<” use when shifts all of the value of the bits to the left side operand.

Example:

Left operand << num

 

  • The Right Shift (>>)

The right shift operator “>>” use when shifts all of the bits value to the right side operand.

Example:

value >> right operand

 

  • The Unsigned Shift Right Operator / Shift right zero-fill (>>>)

when the programmer is working with pixel-based values or graphics. In this condition, the programmer shifts a zero bit into the high order bits. This technique is known as an unsigned shift. Use “>>>” operator to shifts zero into the higher-order bit.

  • Bitwise Operator Assignments

All binary bitwise operators have a shorthand form, which combines the assignment operator with the bitwise operation. For example, two statements that shift the value in c by four bits are equivalent.

   c = c >> 4;
   c >>= 4;

In another hand following two statements, which result in c being assigned the bitwise expression c or m, both are equivalent:

   c = c | m;
   c |= m;

Following the program demonstrate the bitwise operator assignments

   class bitOprAgmt {
      public static void main(String args[]) {
         int aa = 1;
         int bb = 2;
         int cc = 3;
         aa |= 4;
         bb >>= 1;
         cc <<= 1;
         aa ^= cc;
         System.out.println("aa = " + a);
         System.out.println("bb = " + b);
         System.out.println("cc = " + c);
      }
   }

Output:

aa = 3

bb = 1

cc = 6

 

2- Arithmetic Operators

Arithmetic operators commonly used in mathematical expressions and also used in algebra. Operands must be of a numeric type in arithmetic operators. You cannot use boolean types in arithmetic operators.

The below table has arithmetic operators.

Operator Result
+ Addition
Subtraction (also unary minus)
* Multiplication
/ Division
% Modulus
++ Increment
+= Addition assignment
–= Subtraction assignment
*= Multiplication assignment
/= Division assignment
%= Modulus assignment
– – Decrement

 

  • The Basic Arithmetic Operators (+,/,-,*)

The basic arithmetic operations are addition, division, subtraction, and multiplication all these operations work for all numeric data types. The following program demonstrates the arithmetic operators. It also demonstrates the concept of integer division and floating-point division.

  // The basic arithmetic operators example program.
  class BasicOpp {
    public static void main(String args[]) {
      System.out.println("Arithmetic Integer Operators");
        int v = 2 + 3;
        int w = v * 4;
        int x = w / 5;
        int y = x - v;
        int z = -y;
        // all above operators are arithmetic
        System.out.println("v = " + v);
        System.out.println("w = " + w);
        System.out.println("x = " + x);
        System.out.println("y = " + y);
        System.out.println("z = " + z);
        // arithmetic using doubles data types
        System.out.println(" \n Floating Point Arithmetic Variables ");
        double dv = 1 + 2;
        double dw = dv * 4;
        double dx = dw / 5;
        double dy = dx - a;
        double dz = -dy;
        System.out.println("dv = " + dv);
        System.out.println("dw = " + dw);
        System.out.println("dx = " + dx);
        System.out.println("dy = " + dy);
        System.out.println("dz = " + dz);
      }
   }

When you execute this above program output will:

Arithmetic Integer Operators

v = 5

w = 20

x = 4

y = -1

z = 1

Floating Point Arithmetic Variables

dv = 3.0

dw = 12.0

dx = 2.4

dy = -0.6

dz = 0.6

 

  • The Modulus Operator (%)

The modulus operator “%” returns the remainder of a division result operations. It can be applied to floating-point data types and also can be applied to integer types. The following program example of the Modulus operator (%):

   // Modulus operator example program
   class ModulusExp {
      public static void main(String args[]) {
      int a = 12;
      double b = 12.25;
      System.out.println("a mod 11 = " + x % 11);
      System.out.println("b mod 11 = " + y % 11);
      }
   }

When you execute this above example program you will get the following output:

a mod 11 = 1

b mod 11 = 1.25

 

  • Arithmetic Assignment Operators (=)

Assignment operators used to assign value to the variables.

Example:

   int m;
   m = m + 10;

In Java statement as shown below equal to the above statement.

  int m;
  m += 10;

both statements perform the same functions.

The below sample, program show some assignment operators.

   // Example code assignment operators.
   class AsgOpp {
       public static void main(String args[]) {
       int x = 1;
       int y = 2;
       int z = 3;
       x += 5;
       y *= 4;
       z += x * y;
       z %= 6;
       System.out.println("x = " + x);
       System.out.println("y = " + y);
       System.out.println("z = " + z);
       }
   }

Output:

x = 6

y = 8

z = 3

 

  • Increment and Decrement (++,–)

In Java “++” operator called increment and “–” operator called decrement. These operators have some special properties which make them quite interesting. The increment operator increases one (1) in its operand and decrement operator decreases one (1) in its operand. Increment operator increases one by one and decrement operator decrease one by one.

Example 01:

   m = m + 1;
   m++;

both statements are the same.

Example 02:

   m = m – 1;
   m--;

above both statement are also the same

 

3-  Relational Operators

Relational operators show the relationship between the two operands. For example operand A is greater than operand B or operand B is not equal to operand A etc. The result of these operators returns boolean type value. Mostly relational operators used in the expressions that control the conditional statements like if and else.

Some relational operators are shown below:

Operator Result
== Equal to
!= Not equal to
Greater than
Less than
>= Greater than or equal to
<= Less than or equal to

 

  • Equal to (==)

Double assignment operator used to check equality between two operands.

Example:

   int a = 10; 
   int b = 10;
   if (a == b)

 

  • Not equal to (!=)

This operator show results true if two operands are not equal.

Example:

   int a = 10;
   int b = 8;
   if (a != b)

 

  • Greater than (>)

This operator shows result true when the first operand is greater than the second operand otherwise, return false.

Example:

   int a = 10;
   int b = 7;
   if (a > b)

 

  • Less than (<)

This operator shows result true when the first operand is less than the second operand otherwise, return false.

Example:

   int a = 3;
   int b = 10;
   if (a < b)

 

  • Greater than or equal to (>=)

This operator shows result true when the first operand is greater than or equal to the second operand otherwise, return false.

Example:

   int a = 5;
   int b = 5;
   if (a >= b)

 

  • Less than or equal to (<=)

This operator shows result true when the first operand is less than or equal to the other second operand otherwise, return false.

Example:

   int a = 4;
   int b = 5;
   if (a >= b)

4-   Boolean Logical Operators

Boolean logical operators work with boolean values. For example, one operator returns a boolean value and also other operators return a boolean value further, these boolean results values use in boolean logical operators.

Some boolean logical operators are shown below:

Operator Result
& Logical AND
| Logical OR
^ Logical XOR (exclusive OR)
|| Short-circuit OR
&& Short-circuit AND
! Logical unary NOT
&= AND assignment
|= OR assignment
^= XOR assignment
== Equal to
!= Not equal to
?: Ternary if-then-else

 

The logical boolean operators (&, |, ^) used boolean values in the same way which operate on the bits of an integer. Logical NOT operator inverts the boolean stats. For example, value true invert into value false.

Below table shows each the effect of logical operations:

X Y X | Y X & Y X ^ Y !X
False False False False False True
True False True False True False
False True True False True True
True True True True False False

Below Code Example:

   // Operations of the boolean logical operators.
   class BoolOprts {
     public static void main(String args[]) {
     boolean aa = true;
     boolean bb = false;
     boolean cc = aa | bb;
     boolean dd = aa & bb;
     boolean ee = aa ^ bb;
     boolean ff = (!aa & bb) | (aa & !bb);
     boolean gg = !aa;
     System.out.println(" aa = " + aa);
     System.out.println(" bb = " + bb);
     System.out.println(" aa|bb = " + cc);
     System.out.println(" aa&bb = " + dd);
     System.out.println(" aa^bb = " + ee);
     System.out.println("!aa&bb|aa&!bb = " + ff);
     System.out.println(" !aa = " + gg);
     }
   }

Output:

aa = true

bb = false

aa|bb = true

aa&bb = false

aa^bb = true

aa&bb|a&!bb = true

!aa = false

 

Data types

Advertisement