Not equal to integer in java

Java Integer.equals() – Check if two integer values are equal

In this Java tutorial, you will learn about Integer.equals() method, and how to use this method to check if two Integer objects are equal in value, with the help of examples.

Integer.equals()

Integer.equals() compares this integer object to the argument. If both the objects have same integer value, then equals() returns true, else it returns false.

Syntax

The syntax of equals() method is

Parameter Description
obj The other object with which we compare this Integer object to.

Note: The argument you pass can be of any type. You pass int, float, double, etc.

The method returns value of type boolean.

Examples

1. Integer.equals() when both the integers are equal in value

In this example, we will take two integer objects: integer1 and integer2 such that their int values are same. We will check if integer1 equals integer2 using Integer.equals() method. Since, both the objects are same in their value, equals() returns true.

Читайте также:  Switch case for php

Java Program

public class Example < public static void main(String[] args)< Integer integer1 = 7; Integer integer2 = 7; boolean result = integer1.equals(integer2); System.out.println("Result of equals() pb output">Output

Result of equals() = true

2. Integer.equals() when the integers are not equal in value

In this example, we will take two integer objects: integer1 and integer2 such that their int values are not same. We will check if integer1 equals integer2 using Integer.equals() method. Since, both the objects are not same in their value, equals() returns false.

Java Program

public class Example < public static void main(String[] args)< Integer integer1 = 7; Integer integer2 = 8; boolean result = integer1.equals(integer2); System.out.println("Result of equals() pb output">Output

Result of equals() = false

Conclusion

In this Java Tutorial, we have learnt the syntax of Java Integer.equals() method, and also how to use this method with the help of Java example programs.

Источник

How can I properly compare two Integers in Java?

To compare two Integer objects in Java, you can use the equals() method. The equals() method compares the value of the two objects and returns true if they are equal, or false if they are not.

Here is an example of how you can use the equals() method to compare two Integer objects:

Integer num1 = new Integer(5); Integer num2 = new Integer(5); if (num1.equals(num2)) < System.out.println("The numbers are equal."); > else < System.out.println("The numbers are not equal."); >

In this example, the equals() method is used to compare the num1 and num2 Integer objects. Since both objects have the value 5, the equals() method returns true and the message "The numbers are equal." is printed to the console.

You can also use the == operator to compare the values of two Integer objects, but this can be problematic if the objects are not equal.

Integer num1 = new Integer(5); Integer num2 = new Integer(5); if (num1 == num2) < // this may not work as expected System.out.println("The numbers are equal."); > else < System.out.println("The numbers are not equal."); >

The == operator compares the references of the two objects, rather than their values. In this case, the num1 and num2 objects are separate objects that happen to have the same value, so the == operator returns false and the message "The numbers are not equal." is printed to the console.

To compare the values of two Integer objects using the == operator, you can use the intValue() method to get the primitive int value of the objects and then compare the values using the == operator.

Integer num1 = new Integer(5); Integer num2 = new Integer(5); if (num1.intValue() == num2.intValue()) < // this works as expected System.out.println("The numbers are equal."); > else < System.out.println("The numbers are not equal."); >

This code compares the primitive int values of the num1 and num2 objects using the == operator, and the message "The numbers are equal." is printed to the console.

I hope this helps! Let me know if you have any other questions.

Источник

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.

Let us now see the complete example.

Example

Output

Integer 1 = 5 Integer 2 = 5 Integer 3 = 10 val1 is equal to val2 = false val2 is not equal to val3 = true

karthikeya Boyini

I love programming (: That's all I know

  • 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?
  • How to check String for equality under Unicode case folding in Golang?
  • 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, Relational, and Conditional Operators

The equality and relational operators determine if one operand is greater than, less than, equal to, or not equal to another operand. The majority of these operators will probably look familiar to you as well. Keep in mind that you must use " == ", not " = ", when testing if two primitive values are equal.

== equal to != not equal to > greater than >= greater than or equal to < less than 

The following program, ComparisonDemo , tests the comparison operators:

class ComparisonDemo < public static void main(String[] args)< int value1 = 1; int value2 = 2; if(value1 == value2) System.out.println("value1 == value2"); if(value1 != value2) System.out.println("value1 != value2"); if(value1 >value2) System.out.println("value1 > value2"); if(value1 < value2) System.out.println("value1 < value2"); if(value1 >

The Conditional Operators

The && and || operators perform Conditional-AND and Conditional-OR operations on two boolean expressions. These operators exhibit "short-circuiting" behavior, which means that the second operand is evaluated only if needed.

&& Conditional-AND || Conditional-OR

The following program, ConditionalDemo1 , tests these operators:

Another conditional operator is ?: , which can be thought of as shorthand for an if-then-else statement (discussed in the Control Flow Statements section of this lesson). This operator is also known as the ternary operator because it uses three operands. In the following example, this operator should be read as: "If someCondition is true , assign the value of value1 to result . Otherwise, assign the value of value2 to result ."

The following program, ConditionalDemo2 , tests the ?: operator:

Because someCondition is true, this program prints "1" to the screen. Use the ?: operator instead of an if-then-else statement if it makes your code more readable; for example, when the expressions are compact and without side-effects (such as assignments).

The Type Comparison Operator instanceof

The instanceof operator compares an object to a specified type. You can use it to test if an object is an instance of a class, an instance of a subclass, or an instance of a class that implements a particular interface.

The following program, InstanceofDemo , defines a parent class (named Parent ), a simple interface (named MyInterface ), and a child class (named Child ) that inherits from the parent and implements the interface.

class InstanceofDemo < public static void main(String[] args) < Parent obj1 = new Parent(); Parent obj2 = new Child(); System.out.println("obj1 instanceof Parent: " + (obj1 instanceof Parent)); System.out.println("obj1 instanceof Child: " + (obj1 instanceof Child)); System.out.println("obj1 instanceof MyInterface: " + (obj1 instanceof MyInterface)); System.out.println("obj2 instanceof Parent: " + (obj2 instanceof Parent)); System.out.println("obj2 instanceof Child: " + (obj2 instanceof Child)); System.out.println("obj2 instanceof MyInterface: " + (obj2 instanceof MyInterface)); >> class Parent <> class Child extends Parent implements MyInterface <> interface MyInterface <>
obj1 instanceof Parent: true obj1 instanceof Child: false obj1 instanceof MyInterface: false obj2 instanceof Parent: true obj2 instanceof Child: true obj2 instanceof MyInterface: true

When using the instanceof operator, keep in mind that null is not an instance of anything.

Previous page: Assignment, Arithmetic, and Unary Operators
Next page: Bitwise and Bit Shift Operators

Источник

How to Compare two Integers in Java

In Java, “Integer” is a wrapper class by the java.lang package used for constructing integer objects. It stores integer values in 128 bits. While programming in Java, there exists a chance that you need to compare two values of the same data type, such as int. Java offers different methods to compare two Integers; however, the most common method used is the Comparison Operator.

This manual will help you to learn the other methods to compare two integers in Java.

How to Compare two Integers in Java?

For comparing two integers in Java, you can use the below-enlisted methods.

Note: The Comparison operator “==” is used to check equality in primitive data types, while for the objects, the equals() method is used. Whereas the compare() method is useful for both primitive data types and integer-type objects.

Let’s see how these methods will work.

Method 1: Compare two Integers in Java Using Comparison Operator

The most commonly used method by programmers to compare two integers is the Comparison operator “==”. It gives “1” if the specified variables are equal; else, it returns “0”.

Syntax
Follow the below-given syntax for comparing two integers using the Comparison Operator:

Example
In this example, we will create two integer type variables “x” and “y” and initialize them:

Now, compare “x” with “y” using the Comparison Operator in the “if” statements. If the values of both “x” and “y” are the same, the “System.out.println()” method will print the added statement:

if ( x == y ) {
System. out . println ( "Both are equal" ) ;
}
else {
System. out . println ( "x and y are not equal" ) ;
}

The output indicates that the “x” and “y” variable values are not equal:

Let’s move toward the other methods for comparing two integers in Java.

Method 2: Compare two Integers in Java Using equals() Method

In Java, for comparing two objects, use the “equals()” method. It outputs the boolean value “true” if both objects are the same; else, it returns “false”. We can also compare two integer objects as a reference by utilizing the “equals()” method.

Syntax
The equals() method has the following syntax:

The equals() method is called with an “x” integer object and will match its value with “y” that is passed as an argument.

Example
Here, we have two integer object references, “x” and “y,” with values “23” and “23”:

Now, we will compare both variables with the help of the “equals()” method in the added “if” condition:

if ( x. equals ( y ) ) {
System. out . println ( "Both x and y are equal" ) ;
} else {
System. out . println ( "x and y are not equal" ) ;
}

The output shows that the values of “x” and “y” are equal:

Method 3: Compare two Integers in Java Using compare() Method

The “compare()” method can be also utilized to compare two values numerically. It is the static method that belongs to the “Integer” class. It takes two variables as an argument and returns “0” if the first value is smaller than the second, “-1” if the first value is greater than the second, or “1” in the case of equality.

Syntax
The compare() method uses the following syntax for comparing two integers:

The Integer class is utilized for calling the compare() method, and “x” and “y” are the integers passed as arguments.

Example
Here, we have two integer type variables “x” and “y” with values “23” and “20”:

We will compare “x” and “y” by using “compare()” method and store the resultant value in “comp” that is an integer type variable:

Now we will check whether the resultant value of “compare()” method is greater than or less than 0 using the following “if” statement.

if ( comp > 0 ) {
System. out . println ( "x is greater than y" ) ;
} else if ( comp < 0 ) {
System. out . println ( "x is less than y" ) ;
} else {
System. out . println ( "x and y are equal" ) ;
}

The output shows that the value of the “x” variable is greater than “y”:

We have provided all necessary information related to comparing two integers in Java.

Conclusion

For comparing two integers in Java, you can use three methods: the Comparison operator, the equals() method, and compare() method. The Comparison operator “==” is used to check equality in primitive data types, while for the objects, the equals() method is used. Whereas the compare() method is useful for both primitive data types and integer-type objects. This manual illustrated the methods to compare two integers in Java with proper examples.

About the author

Farah Batool

I completed my master's degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.

Источник

Оцените статью