Our social:

Wednesday, October 5, 2016

Relational Operators in Java-Tutorial

Relational Operators

A relational operator compares two values and determines the relationship between those two values.

Operator
Result
Example
Meaning
Return value
==
Equals
a==1
Check if value of variable  a equals to 1
True / False
!=
Not Equals
a!=1
Check if value of variable a not equals to 1
True / False
Greater Than
a>1
Check if value of variable a is greater than 1
True / False
>=
Greater Than or Equals
a>=1
Check if value of variable a is greater than or equals to 1
True / False
Lesser Than
a<1
Check if value of variable a is lesser than 1
True / False
<=
Lesser Than or Equals
a<=1
Check if value of variable a is lesser than or equals to 1.
True / False
Note:
  • Unlike Arithmetic Operators, Relational Operators return Boolean value (either true or false).
  • Relational operators are mostly used while using control blocks in Java. For example – if-else blocks, while loop, do while loop, for loop.
Sample Program for Relational Expressions
Try It
public class RelationalOperators {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int a = 1;
		
		System.out.println("Comparing Equality:: Value of variable a is:: "+ a);
		System.out.println("a==1 ? ::"+ (a==1));
		System.out.println("a==5 ? ::"+ (a==5));
		
		System.out.println();
		
		System.out.println("a>1 ? ::"+ (a>1));
		System.out.println("a>=1 ? ::"+ (a>=1));
		
		System.out.println();
		
		System.out.println("a

The output is shown below or you can click on Try It button.   

Bitwise Operators


Simple Bitwise Operators

Operator
Result
Example
Result
~
Bitwise Unary NOT
int a = ~0;
-1
&
Bitwise AND
int a = 10&41
8
|
Bitwise OR
int a = 10^41
43
^
Bitwise XOR(Exclusive OR)
int a = 10|41
35
<< 
Shift Left
int a = 10<<2
40
>> 
Shift Right
int a = 10>>2
2
>>> 
Shift right Zero fill
int a = -10>>>2
1073741823
In Java, all the Bitwise operators first convert the input in bits, and then work on them, bit by bit.
Explanations of the results in the above table.

Bitwise Unary NOT

Bitwise Unary NOT inverts every bit.
int a = ~0;
int has a size of 32 bit in Java.
Thus, 0 can be represented as
  00000000 00000000 00000000 00000000

So, ~0 represents
  11111111 11111111 11111111 11111111
And, that represents -1 in Java.
Similarly, int a = 1 can be represented as – 
*Note: green boxes represents 1 bit and grey colored boxes represent 0 bit.
That means, ~a means – 
Which is actually -2.

Bitwise AND

AND operation returns 1, if the both the bits are 1, else in any case, it returns 0.
int a = 10&41;
Binary representation of 10 is - 00000000 00000000 00000000 00001010
Binary representation of 41 is - 00000000 00000000 00000000 00101001
                                                 ------------------------------------------------------
AND of 10 and 41 is                  00000000 00000000 00000000 00001000              
And, this represents 8.

Bitwise OR

OR operation returns 1, if any of the bit is 1.
int a = 10|41;
Binary representation of 10 is - 00000000 00000000 00000000 00001010
Binary representation of 41 is - 00000000 00000000 00000000 00101001
                                                 ------------------------------------------------------
OR of 10 and 41 is                     00000000 00000000 00000000 00101011              
And, this represents 43.

Bitwise XOR

XOR returns 1 if exactly one bit in 2 bits is 1
int a = 10^41;
Binary representation of 10 is - 00000000 00000000 00000000 00001010
Binary representation of 41 is - 00000000 00000000 00000000 00101001
                                                 ------------------------------------------------------
OR of 10 and 41 is                     00000000 00000000 00000000 00100011              
And, this represents 35.

Shift Left

Shift left operator shifts every bit of the input to the right.
int a = 10<<2;
This means, every bit of 10 will be shifted to left by 2 bits and the empty spaces will be replaced by 0.
Binary representation of 10 is - 00000000 00000000 00000000 00001010
Therefore, 10<
Which is actually 40 in Integer representation.

Shift Right (Also known as Signed Shift)

Shift left operator shifts every bit of the input to the right.
int a = 10>>2;
This means, every bit of 10 will be shifted to right by 2 bits.
Binary representation of 10 is - 00000000 00000000 00000000 00001010
Therefore, 10>>2 is -                 00000000 00000000 00000000 00000010
Which is actually 2 in Integer representation.
Here the interesting point to note is that, this shift preserves the sign of the input. Thus, as 10 is a positive integer, it preserves the positive sign. Now, let us take an example of a negative number.
Binary representation of -10 is – 11111111 11111111 11111111 11110110
Therefore, -10>>2 is -                 11111111 11111111 11111111 11111101
The bits are shifted to the right, but the empty spaces created in the left side are replaced by 1, to preserve the sign of the input, which is negative.
The result is -3 in Integer representation.

Shift right ZERO (Unsigned Shift)

This difference or >>> operator to >> operator is that: >>> doesn’t preserve the sign of the input. It, replaces the empty places created of the left side while shifting with zeros.
Let us, the same example of -10 with operator >>>.
Binary representation of -10 is – 11111111 11111111 11111111 11110110
Therefore, -10>>>2 is -               00111111 11111111 11111111 11111101
Which is 1073741821 in Integer representation
So,  we have complete our Concepts of Java Operators!

        

 Java String (Part 1)

0 comments:

Post a Comment

Hello Every One thanks For Visit My site You Can Suggest any Idea About site And Related Education Information Provide me Its Best Content For you am Include in this Site And feel free contact And email now I hope This site very help full you