I came across this code which is using .toCharArray() to create a hashcode of a string which will be later used to store data in a HashMap.
public static int generateHashCode(String s) {
int code = 0;
for(char next_character: s.toCharArray()) {
code += next_character;
}
return code;
}
What I understand from this code is that it's using an enhanced for loop to traverse through each character of the string(Made array). What I don't understand is that how it's adding those "characters" to an "integer"-code. I changed the code to just print next_character and it printed all the individual characters of the strings.
What’s happening here is that the char is being promoted to an int, its value will be equal to the ASCII value.