File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

char ' ' space vs. String " " space

 
Pete Johnston
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic