When i was dealing with threads concept in Java, i have seen Thread.java source file. I noticed when setName() method assigns string to a character array called "name[]". Java has a feature of String data type, then why they are using character array.
In source file it is initialised like,
private char name[]; // why not "private String name;"
In setName() method,
public final void setName(String name) {
checkAccess();
this.name = name.toCharArray();
}
Please help me. Thanks in advance.
This name is accessed from native code, so it is easier to handle char arrays than mangle with Java types. The
core-lib-devsmailing list discussed this question a while ago, here’s a link to one mail from the thread. The original question stated that “a fair amount of time goes to that Thread.setName call which I believe a significant portion is to do new char allocation and copy char array etc”. Quoting bits of the answer: