Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • 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

image – Make Selenium Easy Uncategorized
Make Selenium Easy – Page 10 Uncategorized
image – Make Selenium Easy Uncategorized
rolling file appender in log4j2 Uncategorized
September 5, 2018 – Make Selenium Easy Uncategorized
Hierarchy of Selenium Classes and Interfaces – Make Selenium Easy Uncategorized

Recent Posts

  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing
  • Baby Steps To Become Efficient Selenium-Java Automation Tester
  • Features of Selenium 4.0.0 Release – Java Binding
  • Part 1: Handling Drop-down Created Using SELECT Tag In Selenium

Recent Comments

No comments to show.

Archives

  • 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
  • Uncategorized

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark