【发布时间】:2018-06-13 21:44:34
【问题描述】:
我所说的空格是指:
myString = ""; //This string is empty. It is not what I am talking about.
myString = " "; //This string has one space. It is what I am talking about.
现在,我尝试通过执行以下操作为 char 分配一个空格:
char myChar = " ";
我收到以下错误:
incompatible types: String cannot be converted to char.
基于此,我猜引号内的任何内容在 Java 中始终被视为字符串(如果我错了,请纠正我)。现在我只是在做以下事情,它的工作原理:
String emptyStr = " ";
char empty = emptyStr.charAt(0); //Tested it and the char does take a space value.
我的问题如下。 是否有更有效或已经预先确定的方法来做到这一点?
【问题讨论】:
-
" "是String而不是Char。 -
char myChar = ' ';这是默认设置。注意单引号。 -
也许你应该开始咨询Java Language Specification 而不是猜测......对于理解原始类型和文字至关重要。
-
糟糕。我现在很抱歉我的问题在网页服务器中占用了不必要的空间。感谢您的快速回答。