Reputation: 34149
In Thread.java, line 146, I have noticed that the author used a char[]
instead of a String
for the name field. Are there any performance reasons that I am not aware of? getName()
also wraps the character in a String before returning the name. Isn't it better to just use a String
?
Upvotes: 9
Views: 1648
Reputation: 941
Maybe the reason was security protection? String can be changed with reflection, so the author wants copy on read and write. If you are doing that, you might as well use char array for faster copying.
Upvotes: 1
Reputation: 19177
Hard to say. Perhaps they had some optimizations in mind, perhaps the person who wrote this code was simply more used to the C-style char*
arrays for strings, or perhaps by the time this code was written they were not sure if strings will be immutable or not. But with this code, any time a Thread.getName()
is called, a new char array is created, so this code is actually heavier on the GC than just using a string.
Upvotes: 2
Reputation: 403481
In general, yes. I suspect char[]
was used in Thread
for performance reasons, back in the days when such things in Java required every effort to get decent performance. With the advent of modern JVMs, such micro-optimizations have long since become unimportant, but it's just been left that way.
There's a lot of weird code in the old Java 1.0-era source, I wouldn't pay too much attention to it.
Upvotes: 6