Reputation: 691
In the Android API http://developer.android.com/guide/topics/data/data-storage.html#pref
It says:
Shared Preference allows you to save and retrieve persistent key-value pairs of primitive data types. You can use SharedPreferences to save any primitive data: booleans, floats, ints, longs, and strings.
Is String a primitive data type or an Object?
Upvotes: 22
Views: 47672
Reputation: 8328
As far as Java
programming language is considered,
A primitive type is predefined by the language and is named by a reserved keyword.
In addition to the eight primitive data types listed above, the Java programming language also provides special support for character strings via the
java.lang.String
class.
—— from The Java™ Tutorials - Primitive Data Types
So, as such in Java
books, it's not a keyword and not a primitive either. SharedPreferences
may still call it one of the primitives, but that's not from the book of Java
as such, it could be because it's one of the set of basic types like int, float, char etc we come across.
Upvotes: 25
Reputation: 94
Strings are objects of the String class (java.lang.String). In other programming languages, the String, as you know it, was not provided through the language, the user had to use an array of characters to represent the String. This is why a String is not a primitive data type, it is instead a myriad of attributes (int length, char[position], etc.).
Because of the importance of the String, the creators of Java allowed a String to be simply made by String message = Ḧello World;
Nothing wrong with that, many people make objects of (instantiate) the String class that way...However you can also say...char[] arr = {'a','b','c'};
String myString = new String(arr);
That is the same as String myString = ¨abc¨;
This is because as aforementioned, a string is just a series of characters. There is, inside of the String class, a constructor (the thing that follows the new keyword and matches the class name) that accepts as a parameter, an array of characters.
Short answer : String is a class not a primitive data type, When making a String object, you are instantiating the String class
Upvotes: 1
Reputation: 41965
Straight from JLS:
A string literal is a reference to an instance of class String
So no it is not a primitive.
Upvotes: 24
Reputation: 967
String is an object, in android or java it isn't a primitive type at all. you can use strings to store in SharedPreferences.
Upvotes: 4
Reputation: 976
When using Android SharedPreferences you will use getString and putString (with SharedPreferences.Editor) in which case both are Java String Objects. The Java documentation explains that a String isn't technically a primitive, but because it is often treated as one syntactically and it's prevalence it may sometimes be called a primitive. Android probably uses this definition (see http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html)
Upvotes: 4
Reputation: 2126
String is an object, although it can be used in SharedPreferences. String is also stored as a key value pair
Upvotes: 0