Is String a primitive or an Object in Android or Java?-ThrowExceptions

Exception or error:

In the Android API

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?

How to solve:

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.


Straight from JLS:

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

So no it is not a primitive.


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


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


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


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

Leave a Reply

Your email address will not be published. Required fields are marked *