Java 方法调用转换不能转换实参
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18624961/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
actual argument cannot be converted by method invocation conversion
提问by Thufir
What is the correct way to add a char
to charQueue
which is a final ConcurrentLinkedQueue<Character>
parameter?
什么是增加一个正确的方式char
来charQueue
这是一个final ConcurrentLinkedQueue<Character>
参数?
Oracle seems to say that it shouldwork:
Oracle 似乎说它应该可以工作:
From type char to type Character
从字符类型到字符类型
http://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.1.7
http://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.1.7
code:
代码:
package telnet;
import static java.lang.System.out;
import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.logging.Logger;
public class InputStreamWorker {
private final static Logger LOG = Logger.getLogger(InputStreamWorker.class.getName());
public InputStreamWorker() {
}
public void print(final InputStream inputStream, final ConcurrentLinkedQueue<Character> charQueue) {
Thread print = new Thread() {
StringBuilder sb = new StringBuilder();
@Override
public void run() {
try {
char ch = (char) inputStream.read();
sb.append(ch);
while (255 > ch && ch >= 0) {
charQueue.add(ch);
ch = (char) inputStream.read();
System.out.print(ch);
}
} catch (IOException ex) {
out.println("cannot read inputStream:\t" + ex);
}
}
};
print.start();
}
}
Extract from build results:
从构建结果中提取:
-do-compile:
[mkdir] Created dir: /home/thufir/NetBeansProjects/TelnetConsole/build/empty
[mkdir] Created dir: /home/thufir/NetBeansProjects/TelnetConsole/build/generated-sources/ap-source-output
[javac] Compiling 11 source files to /home/thufir/NetBeansProjects/TelnetConsole/build/classes
[javac] /home/thufir/NetBeansProjects/TelnetConsole/src/telnet/InputStreamWorker.java:28: error: no suitable method found for add(char)
[javac] charQueue.add(ch);
[javac] ^
[javac] method ConcurrentLinkedQueue.add(Character) is not applicable
[javac] (actual argument char cannot be converted to Character by method invocation conversion)
[javac] Note: Some input files use unchecked or unsafe operations.
[javac] Note: Recompile with -Xlint:unchecked for details.
[javac] 1 error
回答by Menthos
You are right, it should work fine as method invocation conversion allows boxing conversion from char
to Character
.
Your code compiles fine on my machine, so I suspect a NetBean specific issue.
您是对的,它应该可以正常工作,因为方法调用转换允许从char
到 的装箱转换Character
。您的代码在我的机器上编译得很好,所以我怀疑是 NetBean 特定问题。
In the meantime, you can explicit the conversion from char
to Character
to please the compiler. This should do the trick:
在此期间,您可以显式从转换char
到Character
取悦编译器。这应该可以解决问题:
char ch = Character.valueOf(inputStream.read());
回答by Thufir
The problem was of my own creation. In the same package was a Character
class which was creating, obvious now, a naming conflict.
问题出在我自己的创作上。在同一个包中有一个Character
类,它正在创建,现在很明显,命名冲突。
Renaming the class to MyCharacter
and checking that the Queue used Character
has fixed the bug.
将类重命名为MyCharacter
并检查使用的队列Character
是否修复了错误。