Home >Java >javaTutorial >Comparison Operators in Java

Comparison Operators in Java

WBOY
WBOYOriginal
2024-08-30 15:19:30661browse

Operators are considered special characters or symbols used to perform certain operations on variables or values (operands). In Java, there are several operators that are used to manipulate variables. It includes Arithmetic operators, Bitwise operators, Comparison operators, Logical operators, Misc. operators, Assignment operators, etc. In this article, we will discuss more details on comparison operators in java.

Comparison Operators in Java

Following are the various comparison operators in Java.

ADVERTISEMENT Popular Course in this category JAVA MASTERY - Specialization | 78 Course Series | 15 Mock Tests
Name of the Operator Operator Example
Equal to = = a= =b
Not equal to != a!=b
Less than < a
Greater than > a>b
Less than or equal to <= a<=b
Greater than or equal to >= a>=b

1. Equal to

This operator checks whether the value on the operator’s left side is equal to the value in the right side.

Example:

import java.util.Scanner;
public class ComparisonExample {
public static void main(String[] args) {
int x, y;
Scanner sc= new Scanner(System.in);
//take the value of x as input from user and store it in variable x
System.out.print("Enter the value of x : ");
x = sc.nextInt();
//take the value of y as input from user
System.out.print("Enter the value of y : ");
//store the value in variable y
y = sc.nextInt();
//checks whether x and y are equal; Return true if it is same, else returns false
System.out.println(x == y);
}
}

Output:

Case 1: x = 3;  y =5;  Returns false as they are not equal

Comparison Operators in Java

Case 2: x = 4;  y =4;  Returns true as they are equal

Comparison Operators in Java

2. Not equal to

This operator checks whether the value on the operator’s left side is not equal to the value on the right side.

Example:

import java.util.Scanner;
public class ComparisonExample {
public static void main(String[] args) {
int x, y;
Scanner sc= new Scanner(System.in);
//take the value of x as input from user and store it in variable x
System.out.print("Enter the value of x : ");
x = sc.nextInt();
//take the value of y as input from user
System.out.print("Enter the value of y : ");
//store the value in variable y
y = sc.nextInt();
//checks whether x and y are not equal; Return true if it is not equal, else returns false
System.out.println(x != y);
}
}

Output:

Case 1: x = 3;  y =4;  Returns true as they are not equal

Comparison Operators in Java

Case 2: x = 3;  y =3;  Returns false as they are equal

Comparison Operators in Java

3. Less than

This operator checks whether the value on the operator’s left side is less than the value on the right side.

Example:

import java.util.Scanner;
public class ComparisonExample {
public static void main(String[] args) {
int x, y;
Scanner sc= new Scanner(System.in);
//take the value of x as input from user
System.out.print("Enter the value of x : ");
//store the value in variable x
x = sc.nextInt();
//take the value of y as input from user
System.out.print("Enter the value of y : ");
//store the value in variable y
y = sc.nextInt();
//Returns true if x is less than y, else false
System.out.println(x < y);
}
}

Output:

Case 1: x = 4;  y =6;  Returns true as x is less than y

Comparison Operators in Java

Case 2: x = 44;  y =32;  Returns false as x is not less than y

Comparison Operators in Java

4. Greater than

This operator checks whether the value on the operator’s left side is greater than the value on the right side.

Example:

import java.util.Scanner;
public class ComparisonExample {
public static void main(String[] args) {
int x, y;
Scanner sc= new Scanner(System.<em>in</em>);
//take the value of x as input from user
System.out.print("Enter the value of x : ");
//store the value in variable x
x = sc.nextInt();
//take the value of y as input from user
System.out.print("Enter the value of y : ");
//store the value in variable y
y = sc.nextInt();
//Returns true if x is greater than y, else false
System.out.println(x > y);
}
}

Output:

Case 1: x = 67;  y =66;  Returns true as x is greater than y

Comparison Operators in Java

Case 2: x = 43;  y =57;  Returns false as x is less than y

Comparison Operators in Java

5. Less than or equal to

This operator checks whether the value on the operator’s left side is less than or equal to the value on the right side.

Example:

import java.util.Scanner;
public class ComparisonExample {
public static void main(String[] args) {
int x, y;
Scanner sc= new Scanner(System.in);
//take the value of x as input from user and store it in variable x
System.out.print("Enter the value of x : ");
x = sc.nextInt();
//take the value of y as input from user and store it in variable y
System.out.print("Enter the value of y : ");
y = sc.nextInt();
//Returns true x is less than or equal to y, else false
System.out.println(x <= y);
}
}

Output:

Case 1: x = 45;  y =45;  Returns true as x is equal to y

Comparison Operators in Java

Case 2: x = 45;  y =54;  Returns true as x is less than y

Comparison Operators in Java

Case 3: x = 45;  y =43;  Returns false as x is greater than y

Comparison Operators in Java

6. Greater than or equal to

This operator checks whether the value on the operator’s left side is greater than or equal to the value on the right side.

Example:

import java.util.Scanner;
public class ComparisonExample {
public static void main(String[] args) {
int x, y;
Scanner sc= new Scanner(System.in);
//take the value of x as input from user
System.out.print("Enter the value of x : ");
//store the value in variable x
x = sc.nextInt();
//take the value of y as input from user
System.out.print("Enter the value of y : ");
//store the value in variable y
y = sc.nextInt();
//Returns true x is greater than or equal to y, else false
System.out.println(x >= y);
}
}

Output:

Case 1: x = 54;  y =67;  Returns false as x is less than y

Comparison Operators in Java

Case 2: x = 45;  y =36;  Returns true as x is greater than y

Comparison Operators in Java

Case 3: x = 55;  y =55;  Returns true as x is equal to y

Comparison Operators in Java

The above is the detailed content of Comparison Operators in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Logical Operators in JavaNext article:Logical Operators in Java