I saw the code below from java tutorial oracle. In order to count the number of words between doorbell
(inclusive) and pickle
(inclusive), the autho
subSet(a, b)
is inclusive of a
but exclusive of b
. Therefore if you want to find a subset that is inclusive of the upper bound pickle
, you have to use the next possible string after pickle
as the (exclusive) upper bound.
You can get this by adding the null character \0
to the end of the string.
In Java, a char
is "really" just an integer between 0
and 65535
, and the compareTo
method of String
sorts characters by these values. To see that \0
is the smallest possible char
value you can print its value like this:
System.out.println((int) '\0'); // Prints 0