• Post Reply Bookmark Topic Watch Topic
  • New Topic

Using a char array in string concatenation  RSS feed

 
Rahul Kakkar
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay got it, thanks. I just saw the bytecode and confirmed it. Should've have done that prior to posting here, but anyway.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!