gpt4 book ai didi

java - 为什么我们不能将char类型变量的默认值设置为 ' '

转载 作者:行者123 更新时间:2023-11-29 05:25:54 25 4
gpt4 key购买 nike

为什么我们不能将char类型变量的默认值设置为' '?同样,我们可以在 Java 中将字符串变量的默认值设置为 ""。

假设

class abc
{
private String a="";
private char b='';//here it will give error
}

最佳答案

因为 '' 不是 Java Language Specification 中定义的字 rune 字.

CharacterLiteral:
' SingleCharacter '
' EscapeSequence '

SingleCharacter:
InputCharacter but not ' or \

InputCharacter:
UnicodeInputCharacter but not CR or LF

UnicodeInputCharacter:
UnicodeEscape
RawInputCharacter

UnicodeEscape:
\ UnicodeMarker HexDigit HexDigit HexDigit HexDigit

UnicodeMarker:
u
UnicodeMarker u

RawInputCharacter:
any Unicode character

HexDigit: one of
0 1 2 3 4 5 6 7 8 9 a b c d e f A B C D E F

另一方面,a String literal is defined as

StringLiteral:
" StringCharacters opt "

注意 opt,表示可选。

关于java - 为什么我们不能将char类型变量的默认值设置为 ' ',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22697297/

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