| Author |
Using a char array in string concatenation
|
Rahul Kakkar
Ranch Hand
Joined: Apr 17, 2005
Posts: 70
|
|
It is recommeded that we should not use a char array in string concatenation. For instance, "hello" + new char[]{'w','o','r','l','d'} is bad. But why is this so?
|
 |
Ken Blair
Ranch Hand
Joined: Jul 15, 2003
Posts: 1078
|
|
Because what you're doing is basically the equivalent of: That will indirectly lead to it using the Object.toString() implementation for the char[] which gives you [C@hashcode instead of an equivalent String. I'm not sure why exactly the compiler doesn't use append(char[]) instead of append(Object) but I'm sure someone else will chime in with that.
|
 |
Rahul Kakkar
Ranch Hand
Joined: Apr 17, 2005
Posts: 70
|
|
okay got it, thanks. I just saw the bytecode and confirmed it. Should've have done that prior to posting here, but anyway.
|
 |
 |
|
|
subject: Using a char array in string concatenation
|
|
|