File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
Author

char ' ' space vs. String " " space

Pete Johnston
Greenhorn

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,
Thanks,
Pete
marc weber
Sheriff

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
sscce.org
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: char ' ' space vs. String " " space