Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • About Us
  • Toggle search form

Java Interview Question 4 – What is HashCode of an Empty String?

Posted on 03/16/2025 By admin

As a part of Java Interview Questions, in this post, we will see an interview question which I faced in an interview, “What is HashCode of an Empty String?“.

HashCode is an integer representation which is typically generated by converting the internal address of an object to integer. Object class contains a native method called hashCode() which does the same. But as a programmer, we should override hashCode() and equals() method as required.

String class also overrides hashCode() method and it calculates hashcode of a string using below formula:-

 s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
 

where s[i] is the ith character of the string, n is the length of the string, and ^ indicates exponentiation.

Code is as below:-

public int hashCode() {
        // 'hash' is a class level private int and 'value' is a char[] to get all characters of string.
        int h = hash;
        if (h == 0 && value.length > 0) {
            char val[] = value;

            for (int i = 0; i < value.length; i++) {
                h = 31 * h + val[i];
            }
            hash = h;
        }
        return h;
    }

Above was basic introduction about hashcode and implementation of hashcode in String class. Now interviewer had asked me “What is HashCode of an Empty String?“.

Hope you can answer this now. HashCode of empty string will be zero. In above code, ‘hash’ is a class level private int and ‘value’ is a char[] to get all characters of string. If a string does not contain any characters or string is empty, it will return ‘h’ as default value of integer which is Zero.

Example Java Code:-

package Concepts;

public class HashCodeInJava {
        
        public static void main(String[] args) {
                
                String str1 = "";
                String str2 = new String();
                
                
                System.out.println(str1.hashCode());
                System.out.println(str2.hashCode());
        }

}

If you have any doubt, feel free to comment below.If you like my posts, please like, comment, share and subscribe.#ThanksForReading

#HappyLearning

Uncategorized

Post navigation

Previous Post: Advanced TestNG Tutorials 35: How To Pass Multiple Group Names to be Run at Runtime in TestNG XML Using Beanshell
Next Post: Frequently Asked Java Program 12: Java Program To Find Divisors Of Given Number

Related Posts

TestNG Tutorials 17: Can @Test Annotation Be Used For A Class In TestNG? Uncategorized
July 2018 – Page 2 – Make Selenium Easy Uncategorized
Frequently Asked Java Program 18: Java Program to Reverse Every Word of a String Uncategorized
access token Uncategorized
Postman Tutorial Part 54 – Chaining Requests or Data Sharing among requests in Postman Uncategorized
Log4j2 Tutorial 5 – XML Configuration File to Log Into File and Console Using Log4j2 Together Uncategorized

Recent Posts

  • Common Selenium Exceptions and How to Fix Them
  • How to Use WebDriverWait and Expected Conditions Effectively
  • Implicit Wait vs Explicit Wait vs Fluent Wait in Selenium
  • Handling Text Fields, Text Areas, and Input Forms in Selenium
  • How to Handle Checkboxes and Radio Buttons in Selenium

Recent Comments

No comments to show.

Archives

  • August 2026
  • July 2026
  • June 2026
  • May 2026
  • April 2026
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • August 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • May 2022
  • March 2022
  • October 2021
  • April 2021
  • March 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • May 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • January 2018

Categories

  • Getting Started
  • Locators and Elements
  • Uncategorized
  • Waits and Synchronization

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark