Java中的单引号和双引号有区别吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/439485/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a difference between single and double quotes in Java?
提问by pupeno
Is there a difference between single and double quotes in Java?
Java中的单引号和双引号有区别吗?
采纳答案by Yuval Adam
Use single quotes for literal char
s, double quotes for literal String
s, like so:
对文字char
s使用单引号,对文字s使用双引号String
,如下所示:
char c = 'a';
String s = "hello";
They cannot be used any other way around (like in Python, for example).
它们不能以任何其他方式使用(例如在 Python 中)。
回答by VonC
A char is a single UTF-16 character, that is a letter, a digit, a punctuation mark, a tab, a space or something similar.
字符是单个 UTF-16 字符,即字母、数字、标点符号、制表符、空格或类似的东西。
A char literal is either a single one character enclosed in single quote marks like this
字符文字是用单引号括起来的单个字符,如下所示
char myCharacter = 'g';
or an escape sequence, or even a unicode escape sequence:
或转义序列,甚至是 Unicode 转义序列:
char a = '\t'; // Escape sequence: tab
char b = '7' // Escape sequence, octal.
char c = '\u03a9' // Unicode escape sequence.
It is worth noting that Unicode escape sequences are processed very early during compilation and hence using '\u00A' will lead to a compiler error. For special symbols it is better to use escape sequences instead, i.e. '\n' instead of '\u00A' .
值得注意的是,Unicode 转义序列在编译过程中很早就被处理,因此使用 '\u00A' 将导致编译器错误。对于特殊符号,最好使用转义序列,即 '\n' 而不是 '\u00A' 。
Double quotes being for String
, you have to use a "double quote escape sequence" (\"
) inside strings where it would otherwise terminate the string.
For instance:
双引号是 for String
,你必须\"
在字符串中使用“双引号转义序列”(),否则它会终止字符串。
例如:
System.out.println("And then Jim said, \"Who's at the door?\"");
It isn't necessary to escape the double quote inside single quotes.
The following line is legal in Java:
没有必要在单引号内转义双引号。
以下行在 Java 中是合法的:
char doublequote = '"';
回答by Eddy Bayonne
Let's consider this lines of codes (Java):
让我们考虑这行代码(Java):
System.out.println("H"+"A"); //HA
System.out.println('H'+'a'); //169
1) First line is concatenation of H
and A
that will result in HA
(String literal)
1) 第一行是串联的H
,A
这将导致HA
(String literal)
2) Second we are adding the values of two char that according to the ASCII Table H
=72 and a
=97 that means that we are adding 72+97
it's like ('H'+'a')
.
2) 其次,我们将根据 ASCII 表H
=72 和a
=97的两个字符的值相加,这意味着我们将72+97
其添加为('H'+'a')
.
3) Let's consider another case where we would have:
3)让我们考虑另一种情况:
System.out.println("A"+'N');//AN
In this case we are dealing with concatenation of String A
and char N
that will result in AN
.
在这种情况下,我们正在处理 StringA
和 char 的连接,N
这将导致AN
.
回答by Horse_1995
Single quote indicates character and double quote indicates string..
单引号表示字符,双引号表示字符串。
char c='c';
字符 c='c';
'c'-----> c is a character
'c'-----> c 是一个字符
String s="stackoverflow";
字符串 s="stackoverflow";
"stackoverflow"------> stackoverflow is a string(i.e collection if characters)
“stackoverflow”------> stackoverflow 是一个字符串(即集合 if 字符)