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 hashCode() Method

By Chaitanya Singh | Filed Under: java

The hashCode() method returns a hash code for the given integer value.

Syntax of hashCode() method

The following variation of hashCode() method does not accept any argument. It returns the hash code for the int value represented by this Integer object.

Supported versions: Java 1.2 and onwards.

public int hashCode()

An overloaded version of hashCode() method accepts int argument. It returns the hash code for int argument.

Supported versions: Java 1.8 and onwards.

public int hashCode(int val)

hashCode() Parameters

  • val – An int value whose hash code is to be determined.

hashCode() Return Value

  • A hash code for int value represented by this Integer object or hash code for int argument.

Example 1

public class JavaExample {
  public static void main(String[] args)
  {
    //Integer object contains an int value 101
    Integer i = new Integer(101);
    // hash code for int value 101
    int hc = i.hashCode();
    System.out.println("Hash code for Integer object: "+hc);

    int i2 = 151;
    int hc2 = Integer.hashCode(i2);
    System.out.println("Hash code for int value 151: "+hc2);
  }
}

Output:

Java Integer hashCode() method Output

Example 2

import java.util.Scanner;
public class JavaExample {
  public static void main(String[] args)
  {
    Scanner scan = new Scanner(System.in);
    System.out.println("Enter an integer value: ");
    int i = scan.nextInt();

    int hc = Integer.hashCode(i);
    System.out.println("Hash code for entered value: "+hc);
  }
}

Output:

Integer hashCode example

Example 3

public class JavaExample {
  public static void main(String[] args)
  {
    //if an int value is not passed while
    //creating Integer object.
    Integer i = new Integer("hello");
    System.out.println(i.hashCode());
  }
}

Output:

Exception when Integer value is not an int number

Recommended Posts

  • Java Integer getInteger()
  • Java Integer floatValue()
  • Java Integer equals()
  • Java Boolean hashCode()
❮ 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