Hi, can anyone please tell me that why new object of String class hashcode returns 0 as output. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Hi, can anyone please tell me that why new object of String class hashcode returns 0 as output.

Is it because no object is created? String s = new String(); System.out.println(s.hashcode()); Output is 0.

11th Aug 2020, 11:33 AM
Nid
1 Answer
+ 2
The hash code for a String object is computed as : s[0]*31^(n - 1) + s[1]*31^(n - 2) + ... + s[n - 1] For empty string value of "n" is zero hence hash code value is also zero
11th Aug 2020, 11:37 AM
Arsenic
Arsenic - avatar