为什么我们不能将char类型变量的默认值设置为‘'?同样,我们可以在Java中将字符串变量的默认值设置为“”。
假设
class abc
{
private String a="";
private char b='';//here it will give error
}发布于 2014-03-28 03:28:09
因为''不是Java Language Specification中定义的字符文字。
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 "请注意代表optional的opt。
发布于 2014-03-28 03:29:09
字符串基本上是一个字符数组,数组可以是空的,没有任何问题。
由于字符不是数组,因此它不能为空,必须将其设置为有效的“character”值。
发布于 2014-03-28 03:29:36
如果要将char设置为空,请使用:
Character c='\u0000'http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
https://stackoverflow.com/questions/22697297
复制相似问题