我知道字符串文字是对象。 根据
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/