22

I'm just wondering what the benefits/overheads are for using @string rather than hard coding strings within the actual Java code... For Example:

// To get the string resource:
getActivity.setTitle(getString(R.string.my_string));

Is this the best practice for Things like Actionbar titles, Dynamically created button text, ect... Or should I just do this:

// Hardcoded string
getActivity.setTitle("My String");

I know there will be a bit more overhead doing it the first way.. Just not sure what best practice is.

Himanshu
  • 31,810
  • 31
  • 111
  • 133
Gyroscope
  • 3,121
  • 4
  • 26
  • 33

4 Answers4

28

Incase you were unaware as to the actual point of having the @string system please read over the localization documentation. It allows you to easily locate text in your app and later have it translated.

Edit Thanks to Hippo for clearing this up.

Using multiple strings of the same value no matter the method (Strings.xml vs programatically) doesn't seem to have any associated overhead. According to Oracle "All literal strings and string-valued constant expressions are interned" which means that the object is reused rather than re-created if you use it again.

KDEx
  • 3,505
  • 4
  • 31
  • 39
  • 9
    Strings are interned, so I don't think your second comment applies. – AbdullahC Nov 24 '12 at 07:11
  • 1
    @Hippo That's the point I was trying to make there. If the String "Title" was created in one class and in five other classes. Using Strings.xml will refer to the one instance. Rather than in those 5 classes instantiating a new String object. Does that sound accurate? – KDEx Nov 24 '12 at 16:03
  • 1
    Also, later on, you don't have to wade through code files just to make some cosmetic changes and correct spelling mistakes. – S.D. Nov 24 '12 at 16:08
  • @KDEx: I don't think it does. Once a particular String literal is instantiated, it's added to the String pool. Subsequent uses of the same String literal will reference the previously instantiated String. – AbdullahC Nov 24 '12 at 16:36
  • @Hippo according to [Oracle](http://docs.oracle.com/javase/7/docs/api/java/lang/String.html) it looks like in order to take advantage of that you must use the `intern()` method? So that is to say you must explicitly take advantage of the pool. If in five classes I create `private String title = "Title";` and do not use `intern()` I'll have five instances of `"Title"` floating around. Does this sound accurate? – KDEx Nov 24 '12 at 16:41
  • Quoting from the link you've referenced: "All literal strings and string-valued constant expressions are interned." So, no, you won't have five instances, because string literals are _always_ interned. Strings defined using the `new` operator are not automatically interned. – AbdullahC Nov 24 '12 at 18:28
  • 1
    @Hippo thanks for helping to clarify. I made an edit to reflect that. – KDEx Nov 25 '12 at 00:32
4

That way you have a fixed place to alter all your strings within the project. Lets say you used same string in 10 different locations in the code. What if you decide to alter it? Instead of searching for where all it has been used in the project you just change it once and changes are reflected everywhere in the project.

Shakti
  • 1,581
  • 2
  • 20
  • 33
1

Well strings.xml would have to be parsed, wouldn't it? Then I suppose hard coded would be best for performance, though probably unnoticeable at runtime. People do choose to use it though in order to have all the strings in one spot in case there are plans to translate the app.

mango
  • 5,577
  • 4
  • 29
  • 41
0

There are many benefits to setting the strings in a strings.xml file; in a nutshell, it allows you to use the same string in multiple locations, which is good if you somehow need to modify the string later. It also allows you to display the same text in different languages; hardcoding the string doesn't give you all those options.
BTW, you don't need to put every text in the strings.XML file; only the ones that might be used in multiple places in the application.
The general rule for placing strings in the strings.xml file are these:

  • Will it be used in multiple locations?
  • Will it be used in multiple languages?
  • Will it be dynamic or static?

I'm sure there are other reasons, but these are the ones I know.

Hopefully, this helps.

Kneel-Before-ZOD
  • 4,141
  • 1
  • 24
  • 26