This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Java in General and the fly likes Using a char array in string concatenation Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Using a char array in string concatenation" Watch "Using a char array in string concatenation" New topic
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.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Using a char array in string concatenation
 
Similar Threads
Why Error Here?
objects
Doubt.......null?
need help with Assignment 1.1
First Java Program