""(双引号)如何有方法?
How does "" (double quote symbol) have methods?
双引号 (""
) 怎么会有一个方法,即使您实际上没有在字符串中声明它。例如:
System.out.println("".length());
""
是一个对象,但没有在字符串上声明它吗?
还是他知道 Java 上的关键字已经是一个正在制作的字符串?
""
是 java.lang.String
文字。与任何其他 String 对象一样,您可以对其调用方法。
""
是 String
的实例。您可以在其上调用任何 String
方法。它是文字的事实并没有改变以下事实:
A string literal is always of type String (§4.3.3).
字面量与其他String
的区别在于,字面量是在编译时构造的,所有相等的字面量只有一个实例。
空字符串在JLS - 3.10.5. String Literals中提到:
The following are examples of string literals:
"" // the empty string
"\"" // a string containing " alone
"This is a string" // a string containing 16 characters
"This is a " + // actually a string-valued constant expression,
"two-line string" // formed from two string literals
这是一个有效的字符串,一个空字符串。
JLS §3.10.5 解释了什么是字符串文字 ("A string literal consists of zero or more characters enclosed in double quotes.") 并且还说:
A string literal is always of type String (§4.3.3).
它被称为字符串文字,因此您可以使用任何 java.lang.String
方法。
The String class represents character strings. All string literals in
Java programs, such as "abc", are implemented as instances of this
class.
查看 here 中的 API,您可以在其中找到有关字符串和 java.lang.String
方法的详细解释。
双引号 ""
是一个字符串文字,这就是为什么它像 obj String
.
双引号 (""
) 怎么会有一个方法,即使您实际上没有在字符串中声明它。例如:
System.out.println("".length());
""
是一个对象,但没有在字符串上声明它吗?
还是他知道 Java 上的关键字已经是一个正在制作的字符串?
""
是 java.lang.String
文字。与任何其他 String 对象一样,您可以对其调用方法。
""
是 String
的实例。您可以在其上调用任何 String
方法。它是文字的事实并没有改变以下事实:
A string literal is always of type String (§4.3.3).
字面量与其他String
的区别在于,字面量是在编译时构造的,所有相等的字面量只有一个实例。
空字符串在JLS - 3.10.5. String Literals中提到:
The following are examples of string literals:
"" // the empty string "\"" // a string containing " alone "This is a string" // a string containing 16 characters "This is a " + // actually a string-valued constant expression, "two-line string" // formed from two string literals
这是一个有效的字符串,一个空字符串。
JLS §3.10.5 解释了什么是字符串文字 ("A string literal consists of zero or more characters enclosed in double quotes.") 并且还说:
A string literal is always of type String (§4.3.3).
它被称为字符串文字,因此您可以使用任何 java.lang.String
方法。
The String class represents character strings. All string literals in Java programs, such as "abc", are implemented as instances of this class.
查看 here 中的 API,您可以在其中找到有关字符串和 java.lang.String
方法的详细解释。
双引号 ""
是一个字符串文字,这就是为什么它像 obj String
.