String 类中的indexof(int ch)方法的形参列表为什么采用int型而不采用char型?
涉及到了增补字符,因为char型只有65536个字符,不能表示完所有的字符还有一些诸如"火星文"的未知字符,因此用char型来表示是完全不够的,而int类型的范围是- 2 ^ 31 ~2 ^ 31 - 1,足够用来表示已知和未知的字符,并且int跟char海涉及到隐式字符转换,这样用int作为形参不仅没有多费功夫还扩大了indexof的涵盖范围。
例如:
public class Indexof_Test {
public static void main(String[] args) {
String s="hello.wor.ld";
//基本字符与增补字符
//Character.MIN_SUPPLEMENTARY_CODE_POINT 最小的增补字符
System.out.println( s.indexOf(46) ); //indexOf 返回类型位 int ??为什么用int 不用char 涉及增补字符
System.out.println( s.indexOf('.') ); //隐式字符转换
System.out.println( s.lastIndexOf('.') ); //从后往前查找 返回的索引还是从0开始
}
}