gpt4 book ai didi

java - 基于Java语言规范对String Literal的澄清

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:47:12 25 4
gpt4 key购买 nike

在Java语言规范中有提到

A string literal is always of type String (§4.3.3). It is a compile-time error for a line terminator to appear after the opening " and before the closing matching ".

As specified in §3.4, the characters CR and LF are never an InputCharacter; each is recognized as constituting a LineTerminator.

但是我可以创建并运行下面的语句

 public class StringOperation {
public static void main(String ...args){
String first = "\n";
System.out.println(first);
}

}

而且我没有看到任何错误。感谢您的所有见解。

最佳答案

语法 "\n" 是行终止符的有效转义表示,而不是实际的行终止符。 JLS 阻止你做这样的事情:

String alphabet = "abcdefghijklm
nopqrstuvwxyz";

相反,你必须这样做:

String alphabet = "abcdefghijklm" +
"nopqrstuvwxyz";

关于java - 基于Java语言规范对String Literal的澄清,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20459875/

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