Java : Relational Operators


Relational operators also called as comparison operators used to check the relation between two operands and return the result as a boolean value.

For Example:

x < y (x is less than y) this expression will return the result as true if x is less than y, otherwise false.

See Also :

Here is a complete list of relational operators:

Relational Operators Description
> greater than
>= greater than or equal to
== equal to
!= not equal to
<= less than or equal to
< less than

Relation Operator Example

import java.util.Random;

public class RelationalOperatorTest {

	public static void main(String[] args) {
		Random rand = new Random();
	    int i = rand.nextInt(200);
	    int j = rand.nextInt(200);
	    System.out.println("i = " + i);
	    System.out.println("j = " + j);
	    System.out.println("i > j is " + (i > j));
	    System.out.println("i = j));
	    System.out.println("i <= j is " + (i <= j));
	    System.out.println("i == j is " + (i == j));
	    System.out.println("i != j is " + (i != j));

	    System.out.println("(i < 10) && (j < 10) is " + ((i < 10) && (j < 10)));
	    System.out.println("(i < 10) || (j < 10) is " + ((i < 10) || (j < 10)));

	}

}

Output


i = 185
j = 170
i > j is true
i = j is true
i <= j is false
i == j is false
i != j is true
(i < 10) && (j < 10) is false
(i < 10) || (j < 10) is false
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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s