Apps/Gaming

Java Comparison Operators

In Java, comparison operators are used to compare two values ​​in order to run a code block or set a variable’s value based on the result. Hence, they help us to make decisions. In order to do so, the return value of a comparison must be a boolean value of either true or false. This programming tutorial will cover Java’s comparison operators as well as how to combine them to evaluate both simple and complex expressions.

Reading: Best Online Courses to Learn Java

What Are Java’s Relational Operators

In Java, operators that perform comparisons of two operands are called Relational operators. There are a total of six of them:

  • == : Compares two operands for equality, eg, x == y.
  • != : Compares two operands for inequality, eg, x != y.
  • > : Checks if one operand is greater than the other, eg, x > y.
  • <: Checks if one operand is less than the other, eg, x.
  • >=: Checks if one operand is greater than or equal to the other, eg, x >= y.
  • <= : Checks if one operand is less than or equal to the other, eg, x<=y.

Supported data types for relational operators

the == other != Operators can be used with any primitive data types as well as objects.
Meanwhile, the <, >, <=and >= can be used with any primitive numeric data types (or that can be represented as numbers). Hence, they will work with char, byte, short, internaletc., but not with booleans or objects.

Examples of Primitive Data Type and Object Comparisons in Java

Here is some example code showing a class that compares both primitive data types and objects and prints their results to the console in Java:

public class RelationalOperatorExample { public static void main(String args[]) { int a = 10, b = 5; Integer aa = new Integer(a); Integer bb = new Integer(b); System.out.println(“a > b : ” + (a > b)); // true System.out.println(“a < b : " + (a < b)); // false System.out.println("a >= b : ” + (a >= b)); // true System.out.println(“a <= b : " + (a <= b)); // false System.out.println("a = = b : " + (a == b)); // false System.out.println("a != b : " + (a != b)); // true // objects support == and != operators System.out.println(aa == bb); // false System.out.println(aa != bb); // true } }

In the case of objects, the == returns true only if the two objects occupy the same space in memory, and thus reference the same object.

Reading: Java Math Operators

Combining Relational Operators with Conditional Operators in Java

Relational operators may be combined with conditional operators in order to chain multiple comparisons together. Relational operators check the condition and decide the desired result on the basis of both conditions. There are three types of conditional operators in Java:

  • && – Conditional or Logical AND
  • || – Conditional or Logical OR
  • ?: – Ternary operator

Conditional AND operator in Java

The conditional AND operator is applied between two boolean expressions. It is known as a short-circuiting operator because it only evaluates the second expression if the first one is true. It then returns true if both expressions are true; otherwise, it returns false.

Conditional OR operator in Java

Like the conditional ANDthe conditional OR operator is also applied between two boolean expressions. It returns true if either, or both, of the expressions are true; otherwise, it returns false.

Ternary operators in Java

the ternary operator is a bit of an oddball in that it consists of three operands. It is basically a short form of the if-else statement that sets a variable’s value. Many developers find that it makes the code more readable and succinct. the ternary operator’s full syntax in Java is:

result = (condition) ? expression1 : expression2

Hence, it would replace an IF statement such as this one:

int a = 10, b = 5, c; if (a == 10) { // c is set to 20 because the value of a is 10 c = 20; } else { c = 40; }

with this:

int a = 10, b = 5, c; c = (a == 10) ? 20:40; // Displays 20 because the value of a is 10 System.out.println(“Value of c is: ” + c);

Here is an example program that uses all three conditional operators in Java:

public class ConditionalOperatorExample { public static void main(String args[]) { int x=5, y=4, z=7; System.out.println(x>y && x>z || yz) && x

Final Thoughts on Java Comparison Operators

In this programming tutorial we learned how to evaluate expressions using Java’s relational other conditional comparison operators. While best applied to numeric primitive data types, the equals (==) and not equals (!=) operators can be employed to ascertain whether or not two variables point to the same object instance. For more complex thong other objects comparisons, Java provides many specialized methods. We will get to those in a future article.

read more Java programming tutorials and software development tips.

Related posts

Getting started with Kotlin coroutines

TechLifely

Intro to the Crystal Method in Agile Development

TechLifely

DevOps vs Agile: A Deep Dive

TechLifely

Leave a Comment