[英]logic of java in-built method
can anyone tell me , where can I find the logic of java in-built String methods like length()
, tocharArray()
, charAt()
, etc... I have tried decompiler on String.class
but found no logic their. 谁能告诉我,在哪里可以找到java内置String方法的逻辑,例如
length()
, tocharArray()
, charAt()
等...我曾尝试对String.class
反编译,但没有发现它们的逻辑。 I want to a code to count the number of characters of a String without using any in-built String class but I am unable to crack the idea of how to break String into set of characters without using String in-built method.. eg String str = "hello";
我想一个代码来计算一个字符串的字符数,而无需使用任何内置的String类,但我无法破解如何突破成字符串的字符集,而无需使用字符串内置方法的想法..如
String str = "hello";
how to convert this String into 如何将此字符串转换为
'h' , 'e' , 'l' , 'l' , 'o'
and this is not any homework assignment... 这不是任何家庭作业...
please help with regards, himanshu 请帮忙问候,希曼书
I always used http://grepcode.com . 我一直使用http://grepcode.com 。 It usually has source code of methods/objects I'm looking for.
它通常具有我要查找的方法/对象的源代码。 Here is GC for String class String.length()
这是String类String.length()的 GC
EDIT: As for your second question, how to calculate String length. 编辑:至于第二个问题,如何计算字符串长度。 I would use String.toCharArray().
我会使用String.toCharArray()。 I hope you can calculate length of an array.
我希望您可以计算数组的长度。
Whole string data is kept in a private char array
field. 整个字符串数据都保存在私有char
array
字段中。
length()
is just: length()
只是:
public int length()
{
return this.value.length;
}
And charAt(int)
isn't much more complicated: 而且
charAt(int)
并不复杂:
public char charAt(int paramInt)
{
if ((paramInt < 0) || (paramInt >= this.value.length)) {
throw new StringIndexOutOfBoundsException(paramInt);
}
return this.value[paramInt];
}
The method you are looking for separation String chars is toCharArray()
您正在寻找分离字符串字符的方法是
toCharArray()
If you want to decomplie .class
files try using: http://jd.benow.ca/ It has both GUI application and Eclipse IDE plugin. 如果要反编译
.class
文件,请尝试使用: http ://jd.benow.ca/它具有GUI应用程序和Eclipse IDE插件。
You can view OpenJDK source code online . 您可以在线查看OpenJDK源代码。 Make sure you're looking at the right version of the code (library version and revision).
确保您正在查看正确的代码版本(库版本和修订版)。
For example, here's the code of toCharArray()
of jdk8: 例如,这是
toCharArray()
的代码:
public char[] toCharArray() {
// Cannot use Arrays.copyOf because of class initialization order issues
char result[] = new char[value.length];
System.arraycopy(value, 0, result, 0, value.length);
return result;
}
Here's charAt(int index)
: 这是
charAt(int index)
:
public char charAt(int index) {
if ((index < 0) || (index >= value.length)) {
throw new StringIndexOutOfBoundsException(index);
}
return value[index];
}
You can find the source code of String
class here . 您可以在这里找到
String
类的源代码。
You can't work with a String
without using - directly or indirectly - its methods. 如果不直接或间接使用
String
无法使用它。 For example, you can iterate over string characters using charAt(int index)
or create a StringBuilder(String s)
(which calls String.length()
internally). 例如,您可以使用
charAt(int index)
遍历字符串字符,或创建一个StringBuilder(String s)
(内部调用String.length()
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.