Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I use string constants or string literals

By default I use string constants in my code when I have a string of text that will be output to the screen via a messagebox, label, etc. My main reason for doing so is that I do not want these strings to change at runtime and this way nobody really touches them.

My question is:

It seems that from a memory point of view, this approach is creating an object whose lifetime will be decided by the GB whereas a string literal seems more efficient and will be disposed of more quickly. I know this is a rookie question but is there a benefit (no matter how tiny) when using a literal over a constant or should a constant be used in such scenarios.

I know it will not make any noticeable difference in my apps but I am trying to improve myself as a dev and build standards that I keep a reasonable amount of the time. :o)

like image 817
Jason Irwin Avatar asked Dec 01 '22 12:12

Jason Irwin


2 Answers

Neither. The recommended way AFAIK is to use a resources file.

like image 57
Otávio Décio Avatar answered Dec 05 '22 06:12

Otávio Décio


Prefer string constants to string literals, but the reason is to make your code more maintainable, not to improve its performance.

like image 27
Bill the Lizard Avatar answered Dec 05 '22 06:12

Bill the Lizard