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..
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.
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.
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).
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With