Software Development Exam  >  Software Development Notes  >  Basics of Java  >  Java Relational Operators

Java Relational Operators | Basics of Java - Software Development PDF Download

Introduction

Relational operators in Java are used to compare two values or expressions and determine the relationship between them. These operators are essential for making decisions and controlling the flow of a program. In this article, we will explore the various relational operators available in Java, understand their usage, and provide examples to illustrate their functionality.

Relational Operators in Java

Java provides six relational operators:

  • Equal to (==): Checks if two values or expressions are equal.
  • Not equal to (!=): Checks if two values or expressions are not equal.
  • Greater than (>): Checks if the value on the left is greater than the value on the right.
  • Less than (<): Checks if the value on the left is less than the value on the right.
  • Greater than or equal to (>=): Checks if the value on the left is greater than or equal to the value on the right.
  • Less than or equal to (<=): Checks if the value on the left is less than or equal to the value on the right.

Examples and Explanation

Let's dive into some code examples to understand how these relational operators work in practice:

Example 1: Equal to (==) operator

int x = 5;

int y = 7;

boolean result = (x == y);

System.out.println(result);

Output

false

In this example, we compare the values of x and y using the equal to operator (==). Since x is not equal to y, the result is false.

Example 2: Not equal to (!=) operator

int a = 10;

int b = 10;

boolean result = (a != b);

System.out.println(result);

Output

false

In this example, we compare the values of a and b using the not equal to operator (!=). Since a is equal to b, the result is false.

Example 3: Greater than (>) operator

int p = 15;

int q = 10;

boolean result = (p > q);

System.out.println(result);

Output

true

In this example, we compare the values of p and q using the greater than operator (>). Since p is greater than q, the result is true.

Example 4: Less than (<) operator

int m = 5;

int n = 8;

boolean result = (m < n);

System.out.println(result);

Output

true

In this example, we compare the values of m and n using the less than operator (<). Since m is less than n, the result is true.

Example 5: Greater than or equal to (>=) operator

int c = 10;

int d = 10;

boolean result = (c >= d);

System.out.println(result);

Output

true

In this example, we compare the values of c and d using the greater than or equal to operator (>=). Since c is equal to d, the result is true.

Example 6: Less than or equal to (<=) operator

int i = 7;

int j = 10;

boolean result = (i <= j);

System.out.println(result);

Output

true

In this example, we compare the values of i and j using the less than or equal to operator (<=). Since i is less than j, the result is true.

Sample Problems

Now that we have understood the basics of relational operators, let's solve a few sample problems:

Problem 1: Write a Java program to check if a given number is even.

int number = 8;

boolean isEven = (number % 2 == 0);

System.out.println("Is the number even? " + isEven);

Output

Is the number even? true

Problem 2: Write a Java program to check if a student's grade is passing (greater than or equal to 60).

int grade = 75;

boolean isPassing = (grade >= 60);

System.out.println("Is the grade passing? " + isPassing);

Output

Is the grade passing? true

Conclusion

Relational operators in Java are powerful tools for comparing values and making decisions within a program. By using these operators, you can evaluate conditions and control the flow of your code. Remember to use the appropriate relational operator based on your comparison requirements. Practice with different examples and sample problems to strengthen your understanding of these operators.

The document Java Relational Operators | Basics of Java - Software Development is a part of the Software Development Course Basics of Java.
All you need of Software Development at this link: Software Development
60 videos|37 docs|12 tests

Top Courses for Software Development

60 videos|37 docs|12 tests
Download as PDF
Explore Courses for Software Development exam

Top Courses for Software Development

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

Sample Paper

,

Free

,

Java Relational Operators | Basics of Java - Software Development

,

Previous Year Questions with Solutions

,

Java Relational Operators | Basics of Java - Software Development

,

Semester Notes

,

past year papers

,

Summary

,

Important questions

,

video lectures

,

pdf

,

Objective type Questions

,

Viva Questions

,

Java Relational Operators | Basics of Java - Software Development

,

study material

,

ppt

,

practice quizzes

,

mock tests for examination

,

Extra Questions

,

shortcuts and tricks

,

MCQs

,

Exam

;