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..

Community
  • 1
  • 1
kalyan pvs
  • 14,486
  • 4
  • 41
  • 59
  • 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 – Raghunandan Jan 23 '14 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.. – Pankaj Kumar Jan 23 '14 at 12:50
  • strings.xml faster than constant – Dakshesh Khatri Jan 23 '14 at 12:51
  • 1
    http://stackoverflow.com/questions/16433095/does-hard-coding-of-string-affect-performance. check answer by Raghav Sood – Raghunandan Jan 23 '14 at 12:51
  • @user2968888 And how can we test? Do you have any idea – Pankaj Kumar Jan 23 '14 at 12:52
  • @Raghunandan What about memory taken for those variables??if i have a more number?? – kalyan pvs Jan 23 '14 at 12:52
  • @PankajKumar What about memory taken for those variables??if i have a more number?? – kalyan pvs Jan 23 '14 at 12:54
  • No no no.. I am not with "add constant into each class", I would create a constant class and add all constant into that. BUT, if your need is Localization of your app then better to go with resources. – Pankaj Kumar Jan 23 '14 at 13:00
  • @kalyanpvs no much of a difference regarding memory. i guess there little bit of overhead involved with strings.xml but that should not matter coz the difference is not much. – Raghunandan Jan 23 '14 at 13:01
  • @Raghunandan ok..If i declared a 20 String Constants in class as static then memory allocated to them at the starting..so when will that memory will be released?? – kalyan pvs Jan 23 '14 at 13:05
  • @PankajKumar got your point..As per yours.If i declared a 20 String Constants in class as static then memory allocated to them at the starting..so when will that memory will be free?? – kalyan pvs Jan 23 '14 at 13:06
  • 2
    @kalyanpvs http://stackoverflow.com/questions/11908342/garbage-collection-of-objects-referenced-by-static-variables – Raghunandan Jan 23 '14 at 13:06
  • @Raghunandan You cleared my doubt about static memory..Thank you for your support. – kalyan pvs Jan 23 '14 at 13:09
  • @kalyanpvs also check this http://developer.android.com/tools/building/index.html and this http://developer.android.com/guide/topics/resources/accessing-resources.html – Raghunandan Jan 23 '14 at 13:10
  • I think Raghu has answer mine too... thanks raghu – Pankaj Kumar Jan 23 '14 at 13:13
  • @Raghunandan and pankaj both Cleared all my doubts..thank you. – kalyan pvs Jan 23 '14 at 13:15

5 Answers5

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.

Shyam
  • 6,376
  • 1
  • 24
  • 38
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.

Dave G
  • 9,639
  • 36
  • 41
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.

Mr. P
  • 1,387
  • 1
  • 11
  • 25
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.

rabyunghwa
  • 178
  • 3
  • 24
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.

Njuacha Hubert
  • 388
  • 3
  • 14