22

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?

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
Krishna Prasad
  • 693
  • 1
  • 6
  • 19
  • 7
    A string is an Object that gets special handling in Java. It is ***not*** a primitive, but its important enough to the language and programming in general to be treated in *some regards* like one. – Perception Jan 23 '13 at 06:11
  • primitives is: `int`,'float', 'double', 'bool', – Dmitry Zagorulkin Jan 23 '13 at 06:12
  • @Perception: Thanks for reply, but as per android API how it store in the shared preferences(shared preferences store primitive data only). – Krishna Prasad Jan 23 '13 at 06:12
  • 1
    @Krishna - as mentioned by answers below, the Android [SharedPreferences](http://developer.android.com/reference/android/content/SharedPreferences.Editor.html) API allows you to store primitives, Strings, and even sets of Strings. – Perception Jan 23 '13 at 06:17
  • They're just using the term "primitive" loosely because Strings are objects made up of a primitive data type, char. – Kacy Feb 09 '15 at 16:01
  • String is not a Primitive data type. – Rishabh Agarwal Dec 10 '17 at 20:12

6 Answers6

24

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.

Community
  • 1
  • 1
Swapnil
  • 8,201
  • 4
  • 38
  • 57
23

Straight from JLS:

A string literal is a reference to an instance of class String

So no it is not a primitive.

Narendra Pathai
  • 41,187
  • 18
  • 82
  • 120
4

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)

Aea
  • 976
  • 8
  • 10
  • Thanks for your answer, As per Primitive Data Types "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." right? – Krishna Prasad Jan 23 '13 at 06:18
  • So as it says, there are only eight primitive types in `Java` as of now. – Swapnil Jan 23 '13 at 06:19
4

String is an object, in android or java it isn't a primitive type at all. you can use strings to store in SharedPreferences.

Ajit
  • 957
  • 1
  • 8
  • 24
1

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

Brenann
  • 104
  • 6
0

String is an object, although it can be used in SharedPreferences. String is also stored as a key value pair

pvn
  • 2,016
  • 19
  • 33