. Advertisement .
..3..
. Advertisement .
..4..
The following tutorial will guide how to compare two integers in Java by two methods. They are the compare() one and the == comparators.
What is An Integer In Java?
An integer class in Java is a wrapper for primitive type int. This function is built with various tags to deal with an int value. Each object comes with a single int value.
For example:
Integer int b can create an object initialized with the provided value. Here is its syntax:
public Integer (int b)
Parameters:
b: value with which to initialize
The Integer named String s also creates an integer. Yet, this one is initialized with the provided value by string representation. The default radix is 10.
Syntax:
public Integer(String s) throws NumberFormatException
Parameters:
s: string representation of the int value
Throws:
NumberFormatException :
If the string provided does not represent any int value.
Comparison Between Two Integers In Java
Use The equals() Method To Compare Two Integer References
The equals() is a common method to compare two integers. Its result will be true if both objects are equal. On the contrary, the result is false.
Input:
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
Use The equals() Method To Compare Two Integers
Here are an example of using the equals() method to compare two integers:
Input:
public class SimpleTesting{
public static void main(String[] args) {
Integer a = 10;
Integer b = 10;
if(a.equals(b)) {
System.out.println("Both are equal");
}else System.out.println("Not equal");
}
}
Output:
Both are equal
public class SimpleTesting{
public static void main(String[] args) {
Integer a = 150;
Integer b = 150;
if(a==b) {
System.out.println("Both are equal");
}else System.out.println("Not equal");
}
}
Output:
Not equal
Use The compare() Method
The compare() method is perfect for comparing two integers’ numerical values.
Syntax:
int compare(int val1,int val2)
public static int compare(int x, int y)
Parameter :
x: the first int to compare
y: the second int to compare
Return :
This method returns the value zero if (x==y),
if (x < y), then it returns a value less than zero
and if (x > y), then it returns a value greater than zero.
If the return result is -1, val1 is smaller than val2. On the contrary, if the hand, the return value 1 indicates that val1 is equal to val2. The number 1 in the result means that the val1 is greater.
Input:
public class Main {
public static void main(String[] args) {
int val1 = 200;
int val2 = 250;
int val3 = 200;
System.out.println(Integer.compare(val1, val2));
System.out.println(Integer.compare(val1, val3));
}
}
Output:
-1
0
When Should Not Use The == Operators?
The == operators are ideal for comparing a string instead of two integer values. This is because this method is used to check the reference’s equality.
For example, the value in the range of -130 to 130. Thus, when two objects have an equal value belonging to this range, the == comparator will surely return the true result. It only returns false for values outside this range.
Here is an example of this case:
public class UseEqualsToCompareString {
public static void main(String args[]) throws IOException {
String name = "abc"; String surname = "abc";
//this will return true because name and surname points to same string object if(name == surname){
System.out.println("Two Strings are equal by == because they cached in string pool");
}
surname = new String("abc");
//this will result in false as now surname is not a string literal // and points to different object
if(name == surname){
System.out.println("String literal and String created with new() are equal using ==");
}else{
System.out.println("String literal and String created with new() are not equal using ==");
}
//both strings are equal because there content is same if(name.equals(surname)){ System.out.println("Two Strings are equal in Java using equals method because content is same");
}else{
System. out.println("Two Strings are not equal in Java using equals method because content is same");
}
} }
Output:
Two Strings are equal by == because they cached in string pool
String literal and String created with new() are not equal using ==
Two Strings are equal in Java using equals method because content is same
Conclusion
How to compare two integers in Java? The article suggests various ways to do this practice. Both the equals() and == comparators are ideal to compare integers.
However, it is worth noting that the latter is built to check the reference equality of two objects. On the other hand, the former only checks primitive and non-primitive values.
Therefore, choose the suitable methods for comparing different contexts.
Leave a comment