为什么Character.toString没有给出错误The method toString() in the type Object is not unavailable for the arguments (int)
我正在使用 Java 中的字符,并将整数转换为字符串。我从另一个项目复制了一些代码,在那里我没有收到错误,并将其粘贴到我当前的项目中。当我运行代码时,我不断得到相同的信息,“对象类型中的方法 toString() 不适用于参数 (int)”,错误。为什么我只在一个项目中出现此错误?如何修复它?
这是我的代码:
public class main {
public static void main(String[] args) {
int num = 115;
String value = Character.toString(num); //<-- Error here
System.out.println(value);
}
}
I was working with characters in Java and I was converting a integer to a character string. I had copied some code from another project, where I was not getting the error, and pasted it into my current project. When I ran the code and I kept getting the same, The method toString() in the type Object is not applicable for the arguments (int), error. Why do I get this error in only one project and how can I fix it?
Here's my code:
public class main {
public static void main(String[] args) {
int num = 115;
String value = Character.toString(num); //<-- Error here
System.out.println(value);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有一个
Character.toString(int)
在 Java 11 中引入,它接受一个代码点。换句话说,您可能在一个项目中使用 Java 11 或更高版本,而在另一个项目中没有使用,或者您正在针对-release 8
或低于 11 的其他版本进行编译。顺便说一句,您假设您正在“将整数转换为字符串”。这不是
Character.toString(int)
所做的(它将整数 Unicode 代码点转换为表示该代码点的一两个字符串)。如果要将整数转换为字符串,请使用Integer.toString(int)
或 <代码>String.valueOf(int)。There is a
Character.toString(int)
introduced in Java 11, it accepts a code point. In other words, you're probably using Java 11 or higher in one project, and not in the other, or you're compiling against-release 8
or another version below 11.As an aside, you say you're "converting a integer to a character string". That is not what
Character.toString(int)
does (it converts an integer Unicode code point to a one or two character string representing that codepoint). If you want to convert an integer to string, useInteger.toString(int)
orString.valueOf(int)
.