Java – Android Documentation – Is String a Primitive Type? confused

Android Documentation – Is String a Primitive Type? confused… here is a solution to the problem.

Android Documentation – Is String a Primitive Type? confused

I’m reading the Android documentation on Shared Preferences Here they mentioned one thing

You can use SharedPreferences to save any primitive data: booleans, floats, ints, longs, and strings.

As far as I know, there are 8 primitive types in Java. byte、char、short、int、long、float、double、boolean

String is a class under the java.lang package in Java, not a primitive type.

So here’s the problem

Q1。 Why is this thing in the Android documentation Primitive data: string?

Q2。 Does this primitive word used here mean something other than the primitive data type?

Thank you.

Solution

You’re right, the string is not a primitive data type in Java Medium:

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.

Enclosing your character string within double quotes will automatically create a new String object; for example, String s = "this is a string"; .

The String class is not technically a primitive data type, but considering the special support given to it by the language, you’ll probably tend to think of it as such.

However, in this context, “primitive” may not refer to Java primitive types, but to the range from simple to complex.

The page you are referring to has the following points in Storage Quick View:

  • Use sharing preferences for raw data
  • Use internal devices to store private data
  • Use external storage for large datasets that are not private
  • Use SQLite databases for structured storage

This seems to be a scale from simple to complex (raw, large, and structured).

Or it could be that Android just follows the text mentioned in the Java tutorial (see above) and treats the string as a primitive type, although technically it is not.

You know, just like we claim that Java is object-oriented, although fans of Python/Ruby/Smalltalk will argue that the existence of primitive types makes this statement false until your ears fall to protect your sanity 🙂

Anyway, I’m not sure if this matters. Android is primarily specified by Android documentation, with Java documentation as a fallback (as you can see from their different internationalization methods). It is designed to leverage existing Java skills rather than provide an exact same environment.

If the Android documentation refers to strings as raw strings,

it treats them as raw strings, at least as far as what you can use to share preferences.

Related Problems and Solutions