- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check two numbers for equality in Java
To check two numbers for equality in Java, we can use the Equals() method as well as the == operator.
Firstly, let us set Integers.
Integer val1 = new Integer(5); Integer val2 = new Integer(5);
Now, to check whether they are equal or not, let us use the == operator.
(val1 == val2)
Let us now see the complete example.
Example
public class Demo { public static void main( String args[] ) { Integer val1 = new Integer(5); Integer val2 = new Integer(5); Integer val3 = new Integer(10); System.out.println("Integer 1 = "+val1); System.out.println("Integer 2 = "+val2); System.out.println("Integer 3 = "+val3); System.out.println("val1 is equal to val2 = "+(val1 == val2)); System.out.println("val2 is not equal to val3 = "+(val2 != val3)); } }
Output
Integer 1 = 5 Integer 2 = 5 Integer 3 = 10 val1 is equal to val2 = false val2 is not equal to val3 = true
- Related Articles
- Check two ArrayList for equality in Java
- Check two HashMap for equality in Java
- Check two float arrays for equality in Java
- Java Program to check for equality between two strings ignoring the case
- How to compare two ArrayList for equality in Java?
- How to compare two lists for equality in C#?
- How to compare two arrays for equality in Perl?
- Java Program to compare strings for equality
- MongoDB - How to check for equality in collection and in embedded document?
- How to check for equality of three columns by row in R?
- Java Program for Common Divisors of Two Numbers
- Equality of two arrays JavaScript
- Java program to check if binary representations of two numbers are anagram
- How to compare String equality in Java?
- Equality of two 2-D arrays - JavaScript

Advertisements