gpt4 book ai didi

java - Android 文档 - 字符串是原始类型吗?困惑

转载 作者:太空宇宙 更新时间:2023-11-03 13:23:21 25 4
gpt4 key购买 nike

我正在阅读关于 Shared Preferences 的 Android 文档 here他们提到了一件事,

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

据我所知,Java 中有 8 种原始类型。 byte、char、short、int、long、float、double、boolean

String是Java中java.lang包下的一个,不是原始类型。

所以问题来了,

Q1。为什么Android文档里写这个东西是Primitive data: string?

Q2。此处使用的这个原始词是否意味着原始数据类型以外的其他含义?

谢谢。

最佳答案

你说得对,字符串是 not a primitive data type在 Java 中:

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.

但是,在这个上下文中,“原始”可能不是指 Java 原始类型,而是指从简单到复杂的范围。

您引用的那个页面在“存储快速 View ”中有以下几点:

  • 原始数据使用共享首选项
  • 使用内部设备存储私有(private)数据
  • 对非私有(private)的大型数据集使用外部存储
  • 使用 SQLite 数据库进行结构化存储

这似乎是从简单到复杂(原始、大型和结构化)的规模。

或者可能是 Android 只是遵循 Java 教程(见上文)中提到的文本并将字符串视为原始类型,尽管从技术上讲并非如此。

你知道,就像我们声称 Java 是面向对象的一样,尽管 Python/Ruby/Smalltalk 的粉丝会争辩说原始类型的存在使得这种说法是错误的,直到你的耳朵掉下来保护你的理智:-)

无论如何,我不确定这是否重要。 Android 主要由 Android 文档指定,Java 文档作为后备(正如您可以从它们不同的国际化方法中看到的那样)。它旨在利用现有的 Java 技能,而不是提供完全相同的环境。

如果 Android 文档将字符串称为原始字符串,它会将它们视为原始字符串,至少就您可以用于共享首选项的内容而言。

关于java - Android 文档 - 字符串是原始类型吗?困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24277022/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com