How String.charAt(int i) is implemented in Java?

北城余情 提交于 2021-02-07 11:49:13

问题


If I want to check every char in a String using String.charAt(int i), would it count from start every time or it is converted to an array automatically and get the charAt index directly?

Would it be more efficient if I create a char array by String.toCharArray() and then go through the array by index?

Can I check this up in JavaDoc? Where?


回答1:


The JRE is mostly open source. You can download a zip file here or browse online with websites like grepcode.

String.charAt:

public char charAt(int index) {
    if ((index < 0) || (index >= value.length)) {
        throw new StringIndexOutOfBoundsException(index);
    }
    return value[index];
}


来源:https://stackoverflow.com/questions/20893802/how-string-charatint-i-is-implemented-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!