BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java Integer equals() Method

By Chaitanya Singh | Filed Under: java

Java Integer equals(Object obj) method compares this Integer object to the given object obj. It returns true if both the objects contain same int value else it returns false.

Hierarchy:

java.lang Package
    -> Integer Class
        -> equals() Method 

Syntax of equals() method

public boolean equals(Object obj)

equals() Parameters

  • obj – The given object to compare with this Integer.

equals() Return Value

  • It returns true if the objects are same, else it returns false.

Supported java versions: Java 1.2 and onwards.

Example 1

Comparing three Integer objects with each other.

public class JavaExample {
  public static void main(String[] args) {
    Integer i = new Integer(99);
    Integer i2 = new Integer(35);
    Integer i3 = new Integer("99");
    System.out.print("Integer objects i and i2 are equal: ");
    System.out.println(i.equals(i2));
    System.out.print("Integer objects i and i2 are equal: ");
    System.out.println(i.equals(i3));
  }
}

Output:

Java Integer equals() example Output1

Example 2

Comparing two Integer values entered by user.

import java.util.Scanner;
public class JavaExample{
  public static void main(String[] args){
    Integer i, i2;
    Scanner scan = new Scanner(System.in);
    System.out.print("Enter first integer number: ");
    i = scan.nextInt();
    System.out.print("Enter second integer number: ");
    i2 = scan.nextInt();
    if(i.equals(i2)){
      System.out.println("Entered numbers are equal");
    }else{
      System.out.println("Entered numbers are not equal");
    }
  }
}

Output:

Integer equals() method Output2

Example 3

Two given strings are decoded using Integer decode() method and compared using equals() method.

public class JavaExample{
  public static void main(String[] args){
    Integer i, i2;
    String s1 = "1002";
    String s2 = "1002";
    i = Integer.decode(s1);
    i2 = Integer.decode(s2);
    boolean b = i.equals(i2);
    System.out.println("Given Strings contain same int value? "+b);
  }
}

Output:

Java Integer equals Ex Output3

Recommended Posts

  • Java Integer doubleValue()
  • Java Integer divideUnsigned()
  • Java Integer decode()
  • Java Integer compareTo()
  • Java String equals()
❮ Java Integer

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap