我疯了! Character
构造函数是否应该获得e char
参数?
为什么当我尝试编译它时,它会给我一个编译错误:
public class TestClass {
public static void main(String[] args) {
char c = 'a';
Character cObj = new Character(c);
}
}
错误是:
TestClass.java:5: error: constructor Character in class Character cannot be applied to given types;
Character cObj = new Character(c);
^
required: no arguments
found: char
reason: actual and formal argument lists differ in length
1 error
另外,如果我尝试编译它:
public class TestClass {
public static void main(String[] args) {
char c = 'a';
Character cObj = Character.valueOf(c);
}
}
我明白了:
TestClass.java:5: error: cannot find symbol
Character cObj = Character.valueOf(c);
^
symbol: method valueOf(char)
location: class Character
1 error
这是在macOS Sierra 10.12.6上发生的,其中包含最新的Oracle java版本" 1.8.0_144"。
如果我尝试使用OpenJDk 8在Linux上编译相同的代码,则所有编译都应该如此。
我错过了什么?
答案 0 :(得分:0)
因为java支持装箱原语到包装类,你可以做到
Character cObj = 'a';
或
char c = 'a';
Character cObj = c;
但这个事件没有必要工作
char c = 'a';
Character cObj = new Character(c);
确保您输入了正确的Class java.lang.Character