在Java中单引号和双引号之间有区别吗?

时间:2022-09-11 22:29:48

Is there a difference between single and double quotes in Java?

在Java中单引号和双引号之间有区别吗?

4 个解决方案

#1


122  

Use single quotes for literal chars, double quotes for literal Strings, like so:

文字字符使用单引号,文字字符串使用双引号,如下所示:

char c = 'a';
String s = "hello";

They cannot be used any other way around (like in Python, for example).

它们不能以任何其他方式使用(例如在Python中)。

#2


30  

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文字可以是像这样用单引号括起来的单个字符

char myCharacter = 'g'; 

or an escape sequence, or even a unicode escape sequence:

或转义序列,甚至unicode转义序列:

char a = '\t';    // Escape sequence: tab
char b = '\177'   // 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:

双引号表示字符串,您必须在字符串内部使用“双引号转义序列”(\),否则它将终止字符串。例如:

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 = '"';

#3


6  

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我们添加了两个char的值,这意味着我们添加了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.

在本例中,我们处理的是字符串A和char的串联,这将导致一个。

#4


1  

Single quote indicates character and double quote indicates string..

单引号表示字符,双引号表示字符串。

char c='c';

字符c = ' c ';

'c'-----> c is a character

c是一个字符

String s="*";

字符串s =“*”;

"*"------> * is a string(i.e collection if characters)

“*”——> *是一个字符串(i)。e系列如果字符)

#1


122  

Use single quotes for literal chars, double quotes for literal Strings, like so:

文字字符使用单引号,文字字符串使用双引号,如下所示:

char c = 'a';
String s = "hello";

They cannot be used any other way around (like in Python, for example).

它们不能以任何其他方式使用(例如在Python中)。

#2


30  

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文字可以是像这样用单引号括起来的单个字符

char myCharacter = 'g'; 

or an escape sequence, or even a unicode escape sequence:

或转义序列,甚至unicode转义序列:

char a = '\t';    // Escape sequence: tab
char b = '\177'   // 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:

双引号表示字符串,您必须在字符串内部使用“双引号转义序列”(\),否则它将终止字符串。例如:

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 = '"';

#3


6  

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我们添加了两个char的值,这意味着我们添加了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.

在本例中,我们处理的是字符串A和char的串联,这将导致一个。

#4


1  

Single quote indicates character and double quote indicates string..

单引号表示字符,双引号表示字符串。

char c='c';

字符c = ' c ';

'c'-----> c is a character

c是一个字符

String s="*";

字符串s =“*”;

"*"------> * is a string(i.e collection if characters)

“*”——> *是一个字符串(i)。e系列如果字符)