• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

char ' ' space vs. String " " space

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
reply
    Bookmark Topic Watch Topic
  • New Topic