GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • C programming
  • C++ programming
  • Java programming
  • Python programming
  • HTML
  • CSS
  • Codenora IDE (opens in a new tab)
  • About us
  • Pricing
  • Contact us
  • Privacy policy
  • Terms of service
  • Refund and Cancellation

This view

  • Original page
  • New address

GeezerVison is showing www.dremendo.com — Relational or Comparison Operators in Java | Dremendo

  • Dremendo
  • C
  • C++
  • Java
  • Python
  • HTML
  • CSS
  • More
    • Home
    • C Tutorial
    • C++ Tutorial
    • Java Tutorial
    • Python Tutorial
    • HTML Tutorial
    • CSS Tutorial
?>
profile image
Announcements
No new announcements.
mentora icon
Java Programming Tutorial
  • Java Basic Concepts
    • Introduction
    • Variable
    • Data Types
    • Naming Conventions
    • Integer Variable
    • Long Variable
    • Short Variable
    • Byte Variable
    • Float and Double Variables
    • Character Variable
    • Boolean Variable
    • Constant Variable
    • Typecasting
  • Operators in Java
    • Operators
    • Assignment Operators
    • Arithmetic Operators
    • Relational Operators
    • Logical Operators
    • Operator Precedence
  • Input and Output in Java
    • Environment Setup
    • Program Basic Structure
    • Output
    • Input
    • Comments
  • Decision Making in Java
    • Decision Making
    • if Statement
    • if else Statement
    • if else if Statement
    • Conditional Operator
    • switch case Statement
  • Mathematical Functions
    • Mathematical Functions
  • Loops in Java
    • Loops
    • for Loop
    • while Loop
    • do while Loop
  • Arrays in Java
    • One Dimensional Array
    • Two Dimensional Array
  • String in Java
    • String
    • StringBuffer
    • StringBuilder
  • Searching in Java
    • Linear Search
    • Binary Search
  • Sorting in Java
    • Bubble Sort
    • Selection Sort
    • Insertion Sort
  • Function in Java
    • Function
    • Recursive Function
    • Function Overloading
  • OOPs in Java
    • Classes and Objects
    • Instance Variables
    • Instance Methods
    • Access Modifiers
    • Constructor
    • Static Variables
    • Static Methods
    • Inheritance
    • Interface
    • Types of Inheritance
    • Polymorphism
    • Final Class and Method
    • Abstract Class and Method
    • Wrapper Classes
  • Data Structures in Java
    • Stack
    • Queue
    • Circular Queue
    • Double Ended Queue
    • Singly Linked List
    • Doubly Linked List
    • Stack using Linked List
    • Queue using Linked List
  • Packages in Java
    • Packages
  • Exception Handling in Java
    • Exception Handling
    • User-Defined Exception
  • Thread in Java
    • Thread
    • Thread Methods
  • File Handling in Java
    • Text File Handling
    • Binary File Handling
    • Random Access File Handling

bookmark
Table of Contents

    Relational or Comparison Operators in Java

    In this lesson, we will learn what is the relational or comparison operator and how it works in Java programming with some examples.

    What is Relational Operators

    Relational operators in Java, also known as comparison operators, are used to compare the values of two operands. For example, they check if one operand is equal to the other, greater than, or less than the other, and so on.

    Video Explanation

    Click the button below to watch the explanation.

    Please wait while the video is loading....
    video image

    There are 6 types of relational operators in Java and they are:

    • == Equality
    • > Greater than
    • < Less than
    • >= Greater than or equal to
    • <= Less than or equal to
    • != Not equal to

    Now let's see the examples of all the relational operators one by one for more understanding.

    Equality ==

    The equality operator == is used to check if the values of two operands are the same. The result of the == operator is 1 (true) if the values are equal; otherwise, the result is 0 (false) if they are not equal.

    Example
    int x=5, y=5, z=6, a, b;
    a = (x == y);   // result of a is 1 (true) because x=5 is equal to y=5 so relation is true
    b = (x == z);   // result of b is 0 (false) because x=5 is not equal to z=6 so relation is false

    In the example above, the result of a is 1 because the value of x and y is the same so, the relation is correct. On the other hand, the result of b is 0 because the value of x and z is not the same so the relationship is wrong.

    Greater than >

    The greater than > relational operator is used to check if the value of the left operand is greater than the value of the right operand. The result of the > operator is 1 (true) if the left operand is greater; otherwise, the result is 0 (false) if it is not.

    Example
    int x=8, y=5, z=10, a, b;
    a = (x > y);   // result of a is 1 (true) because x=8 is greater than y=5 so relation is true
    b = (x > z);   // result of b is 0 (false) because x=8 is not greater than z=10 so relation is false

    In the example above, the result of a is 1 because the value of x is greater than the value of y so, the relation is correct. On the other hand, the result of b is 0 because the value of x is not greater than the value of z so the relationship is wrong.

    Less than <

    The less than < relational operator is used to check if the value of the left operand is less than the value of the right operand. The result of the < operator is 1 (true) if the left operand is less; otherwise, the result is 0 (false) if it is not.

    Example
    int x=5, y=8, z=3, a, b;
    a = (x < y);   // result of a is 1 (true) because x=5 is less than y=8 so relation is true
    b = (x < z);   // result of b is 0 (false) because x=5 is not less than z=3 so relation is false

    In the example above, the result of a is 1 because the value of x is less than the value of y so, the relation is correct. On the other hand, the result of b is 0 because the value of x is not less than the value of z so the relationship is wrong.

    Greater than or equal to >=

    The greater than or equal to >= relational operator is used to check if the value of the left operand is greater than or equal to the value of the right operand. The result of the >= operator is 1 (true) if the condition is met; otherwise, the result is 0 (false) if it is not.

    Example
    int w=5, x=5, y=3, z=10, a, b, c;
    a = (w >= x);   // result of a is 1 (true) because w=5 is equal to x=5 so relation is true
    b = (x >= y);   // result of b is 1 (true) because x=5 is greater than y=3 so relation is true
    c = (y >= z);   // result of c is 0 (false) because y=5 is neither greater than z=10 nor equal to z=10 so relation is false

    In the example above, the result of a is 1 because the value of w is not greater but equal to the value of x so, the relation is correct. Next, the result of b is 1 because the value of x is greater than the value of y so, the relationship is correct. On the other hand, the result of c is 0 because the value of y is neither greater nor equal to the value of z so the relationship is wrong.

    Less than or equal to <=

    The less than or equal to <= relational operator is used to check if the value of the left operand is less than or equal to the value of the right operand. The result of the <= operator is 1 (true) if the condition is met; otherwise, the result is 0 (false) if it is not.

    Example
    int w=5, x=5, y=7, z=3, a, b, c;
    a = (w <= x);   // result of a is 1 (true) because w=5 is equal to x=5 so relation is true
    b = (x <= y);   // result of b is 1 (true) because x=5 is less than y=7 so relation is true
    c = (y <= z);   // result of c is 0 (false) because y=7 is neither less than z=3 nor equal to z=3 so relation is false

    In the example above, the result of a is 1 because the value of w is not less but equal to the value of x so, the relation is correct. Next, the result of b is 1 because the value of x is less than the value of y so, the relationship is correct. On the other hand, the result of c is 0 because the value of y is neither less nor equal to the value of z so the relationship is wrong.

    Not equal to !=

    The not equal to != relational operator is used to check if the value of the left operand is not equal to the value of the right operand. The result of the != operator is 1 (true) if the values are not equal; otherwise, the result will be 0 (false) if the values are equal.

    Example
    int x=5, y=8, z=5, a, b;
    a = (x != y);   // result of a is 1 (true) because x=5 is not equal to y=8 so relation is true
    b = (x != z);   // result of b is 0 (false) because x=5 is equal to y=5 so relation is false

    In the example above, the result of a is 1 because the value of x is not equal to the value of y so, the relation is correct. On the other hand, the result of b is 0 because the value of x is equal to the value of z so the relationship is wrong.

    Test Your Knowledge

    Attempt the questions in the exercise to check whether you have understood the lesson clearly.

    Exercise
    Test Your Knowledge
    Previous Next

    We care about your privacy

    Dremendo uses cookies and similar technologies that are necessary for the site to work properly, understand how you interact with our website, and measure the effectiveness of our marketing. By clicking Accept All, you consent to the use of non-essential cookies and similar technologies for analytics, advertising, and ad measurement, as described in our Privacy Policy.

    Cookie Preferences

    Choose which types of cookies Dremendo may use.

    Necessary Cookies

    Required for essential website functionality and security.

    Allow
    Analytics Cookies

    Help us understand how visitors use Dremendo.

    Allow
    Marketing Cookies

    Help us measure advertising performance and understand interactions with our advertisements.

    Allow
    Mentora Question Asked: 0 / 20

    Hi, I’m Mentora!

    I’m your personal AI tutor from Dremendo. Ask me anything, and I’ll help you understand it in a simple and easy way.

    You are visitor 01402503 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.