CoderSathi
  • Tutorial
    • Java Tutorial
    • Swing Tutorial
    • JDBC Tutorial
    • Java String Tutorial
    • Servlet and JSP Tutorial
  • Mongo DB
  • AWS
  • DevOps
  • Linux
  • Git
Home > Java > Comparison or Relational Operators in Java

Java Tutorial

  • Introduction
    • What is Java
    • History Of Java
    • Install Java
    • What is JVM
    • JDK vs JRE vs JVM
    • Java Bytecode
    • OOP vs POP
    • Compile and Run Java
  • Tokens, Expressions and Control Structures
    • Primitive data types
    • Integers
    • Floating Points
    • Characters
    • Booleans
    • User Defined Data Type
    • Declarations
    • Constants
    • Identifiers
    • Literals
    • Type Conversion and Casting
    • Variables
    • Default Variable Initialization
    • Command Line Arguments
    • Arrays of Primitive Types
    • Comment Syntax
    • Garbage Collection
    • Expressions
    • Operators
    • Arithmetic Operator
    • Bitwise and Shift Operator
    • Comparison or Relational Operators
    • Logical Operators
    • Assignment Operators
    • Ternary Operator
    • Increment and Decrement Operator
    • Control Statements
  • OOP Concepts
    • Class and Object
    • Create Class Instance
    • Method
    • Abstraction
    • Encapsulation
    • this keyword
    • Constructor
    • Pass by Value
    • Access Modifier/Control
    • Polymorphism
    • Method Overloading vs Method Overriding
    • Recursion
    • Nested and Inner Class
  • Inheritance and Packaging
    • Inheritance
    • extends Keyword
    • super Keyword
    • Object Class
    • Abstract class
    • Final Class
    • Java Package
    • Interface
  • Handling Error/ Exceptions
    • What is Exception
    • Exception Handling Keywords
    • Common Java Errors
    • User Defined Exception
    • Throwing and re-throwing Exception
    • finally Block
  • Strings
    • Java String Tutorial
  • Threads
    • Introduction
    • Create Thread
    • Thread Lifecycle
    • Thread Priority
    • Thread Synchronization
    • Inner Thread Communication
    • Thread Deadlock
  • IO and Streams
    • java.io Package
    • Files and Directories
    • Byte Stream
    • Character Stream
    • Console Input and Output
    • Serializable and Deserializable
  • Core Packages
    • java.lang Package
    • Math
    • Wrapper Classes
    • java.lang.Number
    • Double
    • Float
    • Integers
    • java.lang.Byte
    • java.lang.Short
    • java.lang.Long
    • java.lang.Character
    • java.lang.Boolean
    • java.util package
    • Vector Class
    • Stack Class
    • Dictionary Class
    • Hashtable
    • Enumeration or Enum
    • Generate Random Number
  • Holding Collection of Data
    • Arrays
    • Map
    • List
    • Set
    • Collection Interface
    • Collections Class
    • ArrayList
    • HashSet
    • TreeSet
    • Comparator
  • Java Bean
    • What is Java Bean
    • Advantages and Disadvantages of Java Bean
    • Java Beans API
    • Introspection
    • Java Bean Properties
    • Bound and Constrained Properties
    • BeanInfo Interface
    • Customizers
    • Java Beans Persistence
    • BeanDescriptor

Comparison or Relational Operators in Java

Learn the concepts, implementation details, and practical steps with a clean developer-focused walkthrough.

Yuba Raj Kalathoki
By Yuba Raj Kalathoki
Published: April 4, 2022 ยท 3 min read ยท 0 Comments
Share: in X
Comparison Operators in Java

Comparison or Relational operators in Java are used to compare two values and determine if they are equal, greater than, or less than each other.

Table of Contents

  • List of comparison operators in Java
  • == Operator
  • != Operator
  • > Operator
  • < Operator
  • >= Operator
  • <= Operator
  • Example
    • Output

List of comparison operators in Java

OperatorMeaningExampleDescription
= =Equala == bChecks if a is equal to b
!=Not equala!=bChecks if a is not equal to b
<Less thana<bChecks if a is less than b
>Greater thana>bChecks if a is greater than b
<=Less than or equala<=bChecks if a is less than or equal to b
>=Greater than or equala>=Checks if a is greater than or equal to b
Comparison operators table

== Operator

This operator is used to determine if two values are equal to each other. For example:

int a = 5;
int b = 5;
if (a == b) {
  System.out.println("a and b are equal");
}

!= Operator

This operator is used to determine if two values are not equal to each other. For example:

int a = 5;
int b = 6;
if (a != b) {
  System.out.println("a and b are not equal");
}

> Operator

This operator is used to determine if one value is greater than another. For example:

int a = 5;
int b = 6;
if (a > b) {
  System.out.println("a is greater than b");
}

< Operator

This operator is used to determine if one value is less than another. For example:

int a = 5;
int b = 6;
if (a < b) {
  System.out.println("a is less than b");
}

>= Operator

This operator is used to determine if one value is greater than or equal to another. For example:

int a = 5;
int b = 5;
if (a >= b) {
  System.out.println("a is greater than or equal to b");
}

<= Operator

This operator is used to determine if one value is less than or equal to another. For example:

int a = 5;
int b = 5;
if (a <= b) {
  System.out.println("a is less than or equal to b");
}

Example

public class Test {
    public static void main(String[] args) {
        int a = 10;
        int b = 5;
        boolean x = true;
        boolean y = false;
        boolean result;

        // Integer comparisons
        result = (a == b);
        System.out.println(a + " == " + b + " : " + result); // Output: 10 == 5 : false

        result = (a != b);
        System.out.println(a + " != " + b + " : " + result); // Output: 10 != 5 : true

        result = (a > b);
        System.out.println(a + " > " + b + " : " + result);  // Output: 10 > 5 : true

        result = (a < b);
        System.out.println(a + " < " + b + " : " + result);  // Output: 10 < 5 : false

        result = (a >= b);
        System.out.println(a + " >= " + b + " : " + result); // Output: 10 >= 5 : true

        result = (a <= b);
        System.out.println(a + " <= " + b + " : " + result); // Output: 10 <= 5 : false

        // Boolean comparisons
        result = (x == y);
        System.out.println(x + " == " + y + " : " + result); // Output: true == false : false

        result = (x != y);
        System.out.println(x + " != " + y + " : " + result); // Output: true != false : true
    }
}

Output

10 == 5 : false
10 != 5 : true
10 > 5 : true
10 < 5 : false
10 >= 5 : true
10 <= 5 : false
true == false : false
true != false : true

It is important to note that comparison operators can only be used with certain data types, such as int, double, char, and boolean. They cannot be used with reference types, like String. To compare strings in Java, you can use the .equals() method. For example:

String str1 = "Hello";
String str2 = "Hello";
if (str1.equals(str2)) {
  System.out.println("str1 and str2 are equal");
}

Comparison operators are a fundamental part of programming and are used in many different contexts, including decision making and looping. Understanding how to use these operators is an essential skill for any Java programmer.

Related Posts:

  • Operators in Java
  • Control Statements in Java
  • Arrays in Java
  • Java String Comparison: equals(), ==, and…
  • MySQL Commands for Developers
  • Expressions in Java
Tags:javalanguage-fundamentals
Was this article helpful?
โ† Previous ArticleIncrement and Decrement Operator in Java
Next Article โ†’Iterable to Stream in Java

Recent Posts

  • How to Use AWS CloudFront Signed URLs in Spring Boot?
  • How to Fix SSH Agent Forwarding on macOS: The Ultimate Guide for Developers
  • How to Read AWS Secrets Manager in Spring Boot (Step-by-Step)
  • How to Fix “Public Key Retrieval is not allowed” MySQL JDBC Error
  • Complete Guide to JaCoCo: How to Measure Java Code Coverage Accurately
CoderSathi

Your go-to resource for Java, Spring Boot, Microservices, AWS, and modern development tutorials.

Linkedin

Quick Links

  • About
  • Contact

Popular Topics

  • Java
  • Spring Boot
  • AWS
  • DevOps
  • MongoDB
  • Linux
  • Git
  • How to
ยฉ 2026 CoderSathi. All rights reserved. Privacy Policy ยท Sitemap