Relational Operators in Java

Relational Operators are used to compare two operands in Java. Relational Operators identifies the relationship of an operand over another operand. For example Ram is taller than Bala.



Relational Operators

Java has six types of relational operators to compare operands.

Operator NameOperatorExample
Equals to==x == y
Not equals to!=x != y
Greater than>x > y
Greater than or Equals to>=x >= y
Lesser than<x < y
Lesser than or Equals to<=x <= y


Equals To

Equals to relational operator is used to compare two values are equal or not.

package com.yawintutor;

public class RelationalEqual {
	public static void main(String[] args) {
		int a = 6;
		int b = 6;
		int c = 3;
		boolean d = false; 
		boolean e = false; 
		d = (a == b);
		e = (a == c);

		System.out.println("a value is " + a);
		System.out.println("b value is " + b);
		System.out.println("c value is " + c);
		System.out.println("\n(a == b) is " + d);
		System.out.println("(a == c) is " + e);

	}
}

Output

a value is 6
b value is 6
c value is 3

(a == b) is true
(a == c) is false


Not Equals To

Not equals to relational operator is used to check two values are not the same or not

package com.yawintutor;

public class RelationalNotEqual {
	public static void main(String[] args) {
		int a = 6;
		int b = 6;
		int c = 3;
		boolean d = false; 
		boolean e = false; 
		d = (a != b);
		e = (a != c);

		System.out.println("a value is " + a);
		System.out.println("b value is " + b);
		System.out.println("c value is " + c);
		System.out.println("\n(a != b) is " + d);
		System.out.println("(a != c) is " + e);

	}
}

Output

a value is 6
b value is 6
c value is 3

(a != b) is false
(a != c) is true


Greater Than

Greater than relational operator is used to verify a value is higher than another value

package com.yawintutor;

public class RelationalGreaterThan {
	public static void main(String[] args) {
		int a = 5;
		int b = 6;
		int c = 3;
		boolean d = false; 
		boolean e = false; 
		d = (a > b);
		e = (a > c);

		System.out.println("a value is " + a);
		System.out.println("b value is " + b);
		System.out.println("c value is " + c);
		System.out.println("\n(a > b) is " + d);
		System.out.println("(a > c) is " + e);

	}
}

Output

a value is 5
b value is 6
c value is 3

(a > b) is false
(a > c) is true


Greater Than Or Equals To

Greater than or equals to relational operator is used to verify a value is higher than another value or both or equal

package com.yawintutor;

public class RelationalGreaterThanOrEqual {
	public static void main(String[] args) {
		int a = 5;
		int b = 6;
		int c = 3;
		int d = 5;
		boolean e = false; 
		boolean f = false; 
		boolean g = false; 
		e = (a >= b);
		f = (a >= c);
		g = (a >= d);

		System.out.println("a value is " + a);
		System.out.println("b value is " + b);
		System.out.println("c value is " + c);
		System.out.println("d value is " + d);
		System.out.println("\n(a >= b) is " + e);
		System.out.println("(a >= c) is " + f);
		System.out.println("(a >= d) is " + g);

	}
}

Output

a value is 5
b value is 6
c value is 3
d value is 5

(a >= b) is false
(a >= c) is true
(a >= d) is true


Lesser Than

Lesser than relational operator is used to verify a value is smaller than another value

package com.yawintutor;

public class RelationalLessThan {
	public static void main(String[] args) {
		int a = 5;
		int b = 6;
		int c = 3;
		boolean d = false; 
		boolean e = false; 
		d = (a < b);
		e = (a < c);

		System.out.println("a value is " + a);
		System.out.println("b value is " + b);
		System.out.println("c value is " + c);
		System.out.println("\n(a < b) is " + d);
		System.out.println("(a < c) is " + e);

	}
}

Output

a value is 5
b value is 6
c value is 3

(a < b) is true
(a < c) is false


Lesser Than Or Equals To

Lesser than or equals to relational operator is used to verify a value is smaller than another value or both or equal

package com.yawintutor;

public class RelationalLessThanOrEqual {
	public static void main(String[] args) {
		int a = 5;
		int b = 6;
		int c = 3;
		int d = 5;
		boolean e = false; 
		boolean f = false; 
		boolean g = false; 
		e = (a <= b);
		f = (a <= c);
		g = (a <= d);

		System.out.println("a value is " + a);
		System.out.println("b value is " + b);
		System.out.println("c value is " + c);
		System.out.println("d value is " + d);
		System.out.println("\n(a <= b) is " + e);
		System.out.println("(a <= c) is " + f);
		System.out.println("(a <= d) is " + g);

	}
}

Output

a value is 5
b value is 6
c value is 3
d value is 5

(a <= b) is true
(a <= c) is false
(a <= d) is true



Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *