Java Operators

Java operators are symbols used to command the computer to perform operations on operands. They follow either of the formats shown in the diagram below and are classified into eight different categories which includes Arithmetic, Relational, Logical, Unary, Bitwise and Bit Shift, Assignment Operators.

operator

Arithmetic Operators: These are similar to the basic operators in mathematics. See the list on table below:

Function Operator Example Result (say X=10 and y=5)
Addition + result= x + y 15
Subtraction result = x – y 5
Multiplication * result = x * y 50
Division / result = x / y 2
Modulus(Remainder of the division of two operands) % result = x % y 0

 Relational Operators: These are operators used in comparing two operands and return a boolean value. See the list on table below:

Function Operator Example Result (say X=10 and y=5)
Equal to == x == y 10==5= False
Not Equal To != x != y 10!=5 =True
Greater than > x > y 10 > 5= True
Less Than < x < y 10 < 5= False
Greater than or Equal To >= x >= y 10>=5= True
Less than or Equal To <= x <= y 10<=5= False

 Logical Operators: These are operators that returns a Boolean result that’s based on the Boolean value of one or two other operands or expressions. See the list on table below:

Function Operator Example Example Result (say X=true and y=false)
Logical AND

 

&& x && y true && false = False
Logical OR

 

|| x || y true || false = True
Logical not !    !x !true  = False

  Unary Operators: They operate on a single operand. See the list on table below:

Function Operator Example Result (say X=10)
Unary Plus + result =+x +10 = 10
Unary Minus result = -x -10 = -10
Increment ++ result = x++ 10++ = 11
Decrement result = x– 10– =  9
Logical Complement (for boolean value) ! result =  !false !false = true

 Bitwise and Bit Shift Operators: These are basic operators in mathematics. See the list on table below:

Function Operator Example Example Result (say X=10=1010 and y=5=0101)
Bitwise AND & x & y 1010 &0101 ==> 0000 =0
Bitwise OR | x | y 1010  | 0101 ==> 1111 =15
Bitwise XOR ^ x ^ y 1010 ^ 0101 ==> 1111 =15
Bitwise compliment ~ ~ y ~0101       ==> 1010= -6
Signed left shift (shifts binary of the operand on the left to the left nth time)

n – operand on the right

<< 1 << 3

1 ==> 00000001

i.e. 00000001 << 3

 

00001000 ==> 8

Hence 1<<3 = 8

 

Signed right shift

(shifts binary of the operand on the left to the right nth time)

n – operand on the right

>> 128 >> 1

128 ==> 10000000

i.e.10000000>>1

 

01000000 ==> 64

Hence 128>>1 = 64

 

 Note: For Signed right shift, a binary number that begins with ‘1’ will shift in ‘1’s. A binary number that begins with ‘0’ will shift in ‘0’s.

Also, to confirm ~5 = -6, take the binary representation of 6, take its complement (inverting all the bits) and add one. See steps below:

  • Binary of 6 ==> 0110
  • Its complement ==> 1001
  • Add one to it ==> 1001 +1 ==> 1010 = ~5

Assignment Operators: The assignment operator assigns the value on its right to the operand on its left.  It is denoted by the symbol “=” and an operator. See the list on table below:

Function Operator Example Meaning (say X=10 and y=5)
Addition += x +=y x=10+5    ==> 15
Subtraction -= x -=y x=10-5    ==> 5
Multiplication *= x *=y x=10*5    ==> 50
Division /= x /=y x=10/5     ==> 2
Modulus(Remainder of the division of two operands) %= x %=y x=10%5   ==> 0
Bitwise AND assignment operator &= x &=y x=10&5  ==> 0
Bitwise OR assignment operator |= x |=y x=10 | 5 ==> 15
Bitwise XOR and assignment operator ^= x ^=y x=105 ==> 15
Left shift AND assignment operator <<= x <<=y x=10<<5 ==> 320
Right shift AND assignment operator >>= x >>=y x=10>>5 ==> 0
Simple assignment = x = y x = 5

In my next post, I will be sharing sample codes showing how these operators can be used within the code. Until then, make an effort to practice using these operators … Happy Coding!!!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s