Java – Basic Operators

Java Basic Operators

Basic Java Operators includes the following:

  1. Arithmetic Operators
  2. Relational Operators
  3. Bitwise Operators
  4. Logical Operators
  5. Assignment Operators
  6. Miscellaneous Operators

 

1. Arithmetic Operators

You use arithmetic operators for math statements. Hence, they are used when you need to to some calculation. I provide a list of arithmetic operators below. Also, a brief explanation of each of them.

Let X = 10 and Y = 20

Operator Brief description An example
+ (Addition) Adds values on both sides of the operator. X + Y will give 30
– (Subtraction) Subtracts right-side operand from left-side operand. X – Y will give -10
* (Multiplication) Multiplies values on both side of the operator. X * Y will give 200
/ (Division) Divides left-side operand by right-side operand. Y / X will give 2
% (Modulus) Divides left-side operand by right-side operand and produces the remainder. Y % X will give 0
++ (Increment) Reduces the value of operand by 1. Y++ gives 21
— (Decrement) Reduces the value of operand by 1. Y– gives 19

Table 1.0: List of Arithmetic Operators

 

2. Relational Operators

You use relational operator to test for conditions. Therefore, you see them in conditional statements. Normally with if statements. Table 1.1 gives list of relational operators.

Here X = 10 and Y = 20

Operator Brief description An example
== (is equal to) Tests if the values of two operands are equal or not, if true then condition returns true. (X == Y) is not true.
!= (is not equal to) Tests if the values of two operands are equal or not, if values are not equal then condition returns true. (X != Y) is true.
> (is greater than) Tests if the value of left operand is greater than the value of right operand, if true then condition returns true. (X > Y) is not true.
< (is less than) Tests if the value of left operand is less than the value of right operand, if true then condition returns true. (X < Y) is true.
>= (is greater than or equal to) Tests if the value of left operand is greater than or equal to the value of right operand, if true then condition returns true. (X >= Y) is not true.
<= (is less than or equal to) Tests if the value of left operand is less than or equal to the value of right operand, if true then condition returns true. (X <= Y) is true.

Table 1.1: List of Relational Operators

 

3. Bitwise Operators

We use Bitwise Operators to perform bit-by-bit operations. For that reason, the operands need to be binary numbers.

Let x = 60 and y = 13. Converting them to binary, we then have:

x = 00111100

y = 00001101

  • x&y = 0000 1100
  • x|y = 0011 1101
  • x^y = 0011 0001
  • ~x  = 1100 0011

Table 1.3 provides a list of bitwise operators

Operator Brief description An example
& (bitwise and) Binary AND Operator produces a bit in the result if it exists in both operands. (x & y) will yield 12 which is 0000 1100
| (bitwise or) Binary OR Operator produces a bit if it exists in either operand. (x | y) will yield 61 which is 0011 1101
^ (bitwise XOR) Binary XOR Operator produces the bit if it is set in one operand but not both. (x ^ y) will yield 49 which is 0011 0001
~ (bitwise compliment) Binary Ones Complement Operator is a unary operator and has the effect of ‘switching’ the  bits. (~x ) will yield -61 which is 1100 0011 in 2’s complement form due to a signed binary number.
<< (left shift) Binary Shift Left Operator. The left operands value is moved left by the number of bits given in the right operand. x << 2 will yield 240 which is 1111 0000
>> (right shift) Binary Shift Right Operator. The left operands value is moved right by the number of bits given int the right operand. A >> 2 will yield 15 which is 1111
>>> (zero right fill shift) Right Shift zero fill operator. The left operands value is moved right by the number of bits given int the right operand and shifted values are filled up with zeros. A >>>2 will yield 15 which is 0000 1111

Table 1.2: List of Bitwise Operators

 

4. Logical Operators

Logical operators are used for logical operations.

Let X = true and Y = false, then the Table 1.3 give a list of logical operations on X and Y.

Operator Brief description An example
&& (logical and) This is the Logical AND operator. If both the operands are non-zero, then the condition results in true. (X && Y) is false
|| (logical or) This is the Logical OR Operator. If any of the two operands are non-zero, then the condition results in true. (X || Y) is true
! (logical not) This is the Logical NOT Operator. You use it to reverse the logical state of its operand. If a condition is true then Logical NOT operator will make false and vice versa !(X && Y) is true

Table 1.3: List of Logical Operators

 

5. Assignment Operators

You use assignment operators to assign values to variables. Table 1.4 provides a list of assignment operators.

They are like two-in-on operations. With exception of simple assignment.

Operator Brief description An example
= This is simple assignment operator. Assigns values from right side operands to left side operand. Z = X + Y will assign value of X + Y into Z
+= Add operator together with assignment operator. It adds right operand to the left operand and assigns the result to left operand. Z += X is equivalent to Z = Z + X
-= Subtract operator together with assignment operator. It subtracts right operand from the left operand and assigns the result to left operand. Z -= X is equivalent to Z = Z – X
*= Multiply operator together with assignment operator. It multiplies right operand with the left operand and assigns the result to left operand. Z *= X is equivalent to Z = X * X
/= Divide operator with assignment operator. It divides left operand with the right operand and assigns the result to left operand. Z /= X is equivalent to X = Z / X
%= Modulus operator together with assignment operator. It takes modulus using two operands and assigns the result to left operand. Z %= X is equivalent to Z = Z % X
<<= Left shift AND assignment operator. Z <<= 2 is same as Z = Z << 2
>>= Right shift AND assignment operator. Z >>= 2 is same as Z = Z >> 2
&= Bitwise AND assignment operator. Z &= 2 is same as Z = Z & 2
^= bitwise exclusive OR and assignment operator. Z ^= 2 is same as Z = Z ^ 2
|= bitwise inclusive OR and assignment operator. Z |= 2 is same as Z = Z | 2

Listing 1.4: List of Assignment Operators

 

6. Miscellaneous Operators

Finally, we now consider two miscellaneous operators.

  • Conditional Operator (?:)
  • InstanceOf Operator (instanceof)

 

Conditional Operator

This is also called ternary operator. This is because it uses three operators. You can probably consider it a shortened if statement.

You used it to conditionally assign a value to a variable. It evaluates a condition. If the condition is true, then assign a value, if false, assign alternate value.

The syntax is:

	variable = (condition) ? value_if_true : value_if_false

Take an example in Listing 1.0 below.

public class Tester {

   public static void main(String args[]) {
      int x;
      int y;
      x = 5;
      y = (x == 10) ? 100: 200;
      System.out.println( "x is : " +  y ); // y is assigned 200

      y = (x == 5) ? 100: 200;
      System.out.println( "x is : " + y ); //y is assigned 100
   }
}

Listing 1.0: Example of Conditional Operator

 

I would recommend you try this yourself. Copy and paste the code in your IDE. Then run it to see the output.

 

instanceof Operator

You use this operator only for object reference variables. It tests whether an object is an object of a given class. The syntax is

object_variable instanceof class_name

If the object variable is an instance of the class, it returns true. Otherwise, it returns false.

 

Example is given in Listing 1.1 below

public class Tester {

   public static void main(String args[]) {
	      String website = "Java Tutorials";

	      boolean answer = website instanceof String;
	      System.out.println( answer );    
   }
}

Listing 1.1: Example of instanceof

 

The output of Listing 1.1 would be true. This is because the string website is an instance of the String wrapper class.