8

I have a String is like String data="apps";

i know loading String in Android in two ways..

First one is

so it is a constant i defined it as

public static final String data="apps";

And another type is defing it in res/vslues/strings.xml file like..

<string name="data">apps</string>
<string name="hello_world">Hello world!</string>

if i want to use it..

for the first way ClassName.data

for second way context.getResources().getString(resourceid)

Question:

so now my question is I want to use same String in 30 times in different classes.And I have more number of variables.so which will load faster and take lesser memory in the above methods..

16
  • 1
    prefer the second one. coz if you are using the same in all activities and you want to change it again you need to change it only once in strings.xml. I don't think there will be too much diff in the loading Commented Jan 23, 2014 at 12:50
  • 1
    Depends on where these constant and resources are going to be used. I think for java code, constants are better, because you have no need to use a method to read string value like resources. But resources can be better for locale handling.. Commented Jan 23, 2014 at 12:50
  • strings.xml faster than constant Commented Jan 23, 2014 at 12:51
  • 1
    stackoverflow.com/questions/16433095/…. check answer by Raghav Sood Commented Jan 23, 2014 at 12:51
  • 2
    @kalyanpvs stackoverflow.com/questions/11908342/… Commented Jan 23, 2014 at 13:06

5 Answers 5

13

However, speed shouldn't really be an issue in either case. I would recommend organizing based on what makes sense.

Constants class

Put strings constants that will be used internally, like database column names or other keys.

strings.xml

Put strings that are displayed for the user. This way you can take advantage of localization, etc.

As per requirement you should be prefer second approach means XML based.

Sign up to request clarification or add additional context in comments.

1 Comment

this sounds like the most logical answer to me. thanks
1

The XML string values are meant to be display strings that can be overridden based on locale. This way you can refer to a translatable display value by a constant key.

Comments

1

You should consider using XML strings as a way to display something to the user and change it depending on locale.

Never the less, public static final String data="apps"; should be used in order to hide some not-for-user data like db connections, log messages etc.

Comments

0

I'd suggest that you leverage the Android resource system.

To quote the book Android In Practice: Resources in Android are efficient and fast. XML resources are compiled into a binary format. This makes them friendly at development time, without being slow at Runtime.

Comments

0

I think the speed and memory usage would be the same. The constant in a class is just like a constant in the string.xml which is stored once and referenced whenever you need it elsewhere. However, the advantage of storing in string.xml over the other would be the extra import lines of code which would be avoided. Meanwhile, you have to remember that with string.xml storage you can only use it where the context of an activity is accessible.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.