Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XML string resources vs Java Constant Strings?

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

like image 289
kalyan pvs Avatar asked Jan 23 '14 12:01

kalyan pvs


People also ask

What is the advantage of making strings in strings XML file?

One of the main benefits is for localization: you keep your code language-independent and just need to provide a different XML file for each language you want to support.

Why do we store strings as resources in XML?

The purpose of strings. xml (and other *. xml resource files) is to regroup similar values in one place. This facilitates finding values that would be otherwise buried in the code.

Why should you use string resources instead of hard coded strings in your apps?

It allows you to easily locate text in your app and later have it translated. Strings can be internationalized easily, allowing your application to support multiple languages with a single application package file (APK).

Are strings constant in Java?

One of the most important characteristics of a string in Java is that they are immutable. In other words, once created, the internal state of a string remains the same throughout the execution of the program. This immutability is achieved through the use of a special string constant pool in the heap.


1 Answers

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.

like image 200
sachin10 Avatar answered Nov 15 '22 14:11

sachin10