Java

Relational Operators in Java

In Java, relational operators are operators that compare numbers or characters. Characters are compared depending on the order in the ASCII code. In Java, relational operators are less-than, less-than-or-equal-to, greater-than, greater-than-or-equal-to. Java also has another operator, called the instance-of operator, which is also a relational operator. So there are five relational operators in Java.The operators and their symbols are:

less-than : <

less-than-or-equal-to : <=

greater-than : >

greater-than-or-equal-to : >=

instance-of : instanceof

Both operands of each of these operators have to be of the same type. The result will be unreliable if one operand type is different from the other operand type. That is, both operands should be all ints, or all floats, or all doubles, or all characters.

This article illustrates the use of logical operators, with ints, doubles and letters of the alphabet. It also illustrates the use of instanceof, which is not really meant for primitive types.

Note an operand can be held by a variable.

< Operator

With ints

The following program, shows a use of the < operator with ints:

public class TheClass {
public static void main(String[] args) {
if (2 < 5)
System.out.println("Yes");
else
System.out.println("No");
}
}

The output is, Yes.

With chars

The following program, shows a use of the < operator with chars:

public class TheClass {
public static void main(String[] args) {
if ('B' < 'D')
System.out.println("Yes");
else
System.out.println("No");
}
}

The output is, Yes.

With doubles

The following program, shows a use of the < operator with doubles:

public class TheClass {
public static void main(String[] args) {
if (2.5 < 4.5)
System.out.println("Yes");
else
System.out.println("No");
}
}

The output is, Yes.

<= Operator

With ints

The following program, shows a use of the <= operator with ints:

public class TheClass {
public static void main(String[] args) {
if (5 <= 5)
System.out.println("Yes");
else
System.out.println("No");
}
}

The output is, Yes.

With chars

The following program, shows a use of the <= operator with chars:

public class TheClass {
public static void main(String[] args) {
if ('D' <= 'D')
System.out.println("Yes");
else
System.out.println("No");
}
}

The output is, Yes.

With doubles

The following program, shows a use of the <= operator with doubles:

public class TheClass {
public static void main(String[] args) {
if (4.5 <= 4.5)
System.out.println("Yes");
else
System.out.println("No");
}
}

The output is, Yes.

> Operator

With ints

The following program, shows a use of the > operator with ints:

public class TheClass {
public static void main(String[] args) {
if (5 > 2)
System.out.println("Yes");
else
System.out.println("No");
}
}

The output is, Yes.

With chars

The following program, shows a use of the > operator with chars:

public class TheClass {
public static void main(String[] args) {
if ('D' > 'B')
System.out.println("Yes");
else
System.out.println("No");
}
}

The output is, Yes.

With doubles

The following program, shows a use of the > operator with doubles:

public class TheClass {
public static void main(String[] args) {
if (4.5 > 2.5)
System.out.println("Yes");
else
System.out.println("No");
}
}

The output is, Yes.

>= Operator

With ints

The following program, shows a use of the >= operator with ints:

public class TheClass {
public static void main(String[] args) {
if (5 >= 5)
System.out.println("Yes");
else
System.out.println("No");
}
}

The output is, Yes.

With chars

The following program, shows a use of the >= operator with chars:

public class TheClass {
public static void main(String[] args) {
if ('D' >= 'D')
System.out.println("Yes");
else
System.out.println("No");
}
}

The output is, Yes.

With doubles

The following program, shows a use of the >= operator with doubles:

public class TheClass {
public static void main(String[] args) {
if (4.5 >= 4.5)
System.out.println("Yes");
else
System.out.println("No");
}
}

The output is, Yes.

instanceof Operator

The instanceof operator returns true if a non-primitive object is an instance of a defined class. The object is the left operand, while the class is the right operand.

Defined Class

The following program illustrates this:

class AClass {
}

public class TheClass {
public static void main(String[] args) {
AClass obj = new AClass();
boolean bl = obj instanceofAClass;
System.out.println(bl);
}
}

The output is true.

int and Integer

Integer is the class wrapper for the primitive type, int. The following program shows how the instanceof operator can be used with int and Integer:

public class TheClass {
public static void main(String[] args) {
Integer in = 5;
boolean bl = in instanceofInteger;
System.out.println(bl);
}
}

The output is true. The int has to be a referenced int, which is Integer, and not just int.

float and Float

Float is the class wrapper of the primitive type, float. The following program shows how the instanceof operator can be used with float and Float:

public class TheClass {
public static void main(String[] args) {
Float flt = 2.5f;
boolean bl = fltinstanceofFloat;
System.out.println(bl);
}
}

The output is true. The float has to be a referenced float, which is Float, and not just float.

double and Double

Double is the class wrapper of the primitive type, double. The following program shows how the instanceof operator can be used with double and Double:

public class TheClass {
public static void main(String[] args) {
Double dbl = 3.6;
boolean bl = dblinstanceofDouble;
System.out.println(bl);
}
}

The output is true. The double has to be a referenced double, which is Double, and not just double (lowercase ‘d’).

char and Character

Character is the class wrapper of the primitive type, char. The following program shows how the instanceof operator can be used with char and Character:

public class TheClass {
public static void main(String[] args) {
Character ch = 'A';
boolean bl = chinstanceofCharacter;
System.out.println(bl);
}
}

The output is true. The char has to be a referenced char, which is Character, and not just char.

boolean and Boolean

Boolean is the class wrapper of the primitive type, boolean. The following program shows how the instanceof operator can be used with boolean and Boolean:

public class TheClass {
public static void main(String[] args) {
Character ch = 'A';
boolean bl = chinstanceofCharacter;
System.out.println(bl);
}
}

The output is true. The boolean has to be a referenced boolean, which is Boolean, and not just a boolean.

Conclusion

In Java, relational operators are less-than (<), less-than-or-equal-to (<=), greater-than (>), greater-than-or-equal-to (>=). Java also has another operator, called the instance-of operator (instanceof), which is also a relational operator. The instanceof operator returns true if a non-primitive object is an instance of a defined class. The object is the left operand, while the class is the right operand.

About the author

Chrysanthus Forcha

Discoverer of mathematics Integration from First Principles and related series. Master’s Degree in Technical Education, specializing in Electronics and Computer Software. BSc Electronics. I also have knowledge and experience at the Master’s level in Computing and Telecommunications. Out of 20,000 writers, I was the 37th best writer at devarticles.com. I have been working in these fields for more than 10 years.