File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes char ' ' space vs. String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "char Watch "char New topic

char ' ' space vs. String " " space

Pete Johnston

Joined: Sep 17, 2008
Posts: 10
Say I have a String type variable and I want to concatenate a blank space, I could use: String word = aWord + " ";
or: String word = aWord + ' ';

My question is what is the benefit of using a char space. I would guess it has to do with char being a primitive type variable and String a reference variable? therefore using more memory? and if the program is going to use the concatenated word multiple times this makes a difference?

any insight on this is appreciated,
marc weber

Joined: Aug 31, 2004
Posts: 11343

Consider how the String concatenator works, according to JLS - 15.18.1...
If only one operand expression is of type String, then string conversion is performed on the other operand to produce a string at run time...

A value x of primitive type T is first converted to a reference value as if by giving it as an argument to an appropriate class instance creation expression:

... If T is char, then use new Character(x)...

This reference value is then converted to type String by string conversion.

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
I agree. Here's the link:
subject: char ' ' space vs. String " " space
It's not a secret anymore!