Reputation: 473
I could not find any threads containing information about why the indexOf method allows for different parameters other than an integer.
I tried to check in the javados to see if there was a method with the same name but different parameters but I couldn't find any that allowed an input of characters here: https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#indexOf-int-
However, reading the indexOf method with the int as a parameter I get that:
If a character with value ch occurs in the character sequence represented by this String object, then the index (in Unicode code units) of the first such occurrence is returned.
How is this possible though, I thought that you could only return a type that is the same as the method?
Upvotes: 0
Views: 116
Reputation: 379
One of the indexOf() methods(when we pass a character) is of type int and the parameter is also an int:
int indexOf(int ch)
When we pass a character thru the function, it will be typecasted automatically to the unicode value of the character, which is indeed an integer. The method will return an integer which is the index where the corresponding character first appears in the string.
Upvotes: 1