java - 调用字符串文字的方法 (Java)

标签 java string object

我知道字符串文字是对象。 根据

https://en.wikibooks.org/wiki/Java_Programming/Classes,_Objects_and_Types

When an object is created, a reference to the object is also created. The object can not be accessed directly in Java, only through this object reference. This object reference has a type assigned to it. We need this type when passing the object reference to a method as a parameter.

但是当我们有文字访问字符串方法时,我们是否违反了这一点?

例如:

System.out.println("Literal".toUpperCase());

这不是直接访问对象吗?而不是通过引用访问对象。

例如:

String x = "Literal"; 
System.out.println(x.toUpperCase());

最佳答案

Isn't this directly accessing the object? as opposed to accessing the object through the reference.

不,您仍在使用引用。作为字符串文字的表达式的值是字符串引用。它仍然不是您可以直接访问的对象。

在您的示例中,x 的值仍然是一个引用,并且您的两个代码段是等效的,除了变量 x 的存在。

关于java - 调用字符串文字的方法 (Java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56016665/

相关文章:

javascript - 按位与运算符如何处理 javascript 中的对象?

java - 初始化 Java Web 应用程序

java - TreeMap 的工作,Java 中插入和读取操作的时间复杂度

javascript - 如何使用 `ArrayBuffer` 从 `bytes` 生成 `js_of_ocaml`

Excel VBA宏读取具有不同文本的一列

javascript - 如何在 JavaScript 中更新数组中的特定值

java - 2D array[row][] = str.split ("\t") 未按预期运行

java - GWT:带有按钮单击的往返示例

javascript - 在 JavaScript 中打印一个大整数?

javascript:对象可以字面上替换数组吗?