Is String a primitive or an Object in Android or Java?
Asked Answered
S

6

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?

Scissure answered 23/1, 2013 at 6:8 Comment(6)
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.Aiden
primitives is: int,'float', 'double', 'bool',Schuss
@Perception: Thanks for reply, but as per android API how it store in the shared preferences(shared preferences store primitive data only).Scissure
@Krishna - as mentioned by answers below, the Android SharedPreferences API allows you to store primitives, Strings, and even sets of Strings.Aiden
They're just using the term "primitive" loosely because Strings are objects made up of a primitive data type, char.Electrode
String is not a Primitive data type.Drayman
C
25

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.

Charitacharitable answered 23/1, 2013 at 6:16 Comment(0)
M
24

Straight from JLS:

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

So no it is not a primitive.

Micheal answered 23/1, 2013 at 6:21 Comment(1)
Straight from the bible of JavaNarayan
M
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)

Mcquoid answered 23/1, 2013 at 6:13 Comment(2)
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?Scissure
So as it says, there are only eight primitive types in Java as of now.Charitacharitable
E
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.

Esteban answered 23/1, 2013 at 6:14 Comment(0)
D
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

Disparagement answered 19/10, 2017 at 15:54 Comment(0)
P
0

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

Predisposition answered 23/1, 2013 at 6:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.