为什么我从 java 编译器得到不兼容的类型?
我是一个 Java 新手,但我是一个经验丰富的程序员(汇编器、COBOL、C/C++、REXX)
我无法理解为什么编译器给我以下内容:
[loading java\lang\Throwable.class(java\lang:Throwable.class)]
src\LU62XnsCvr.java:265: incompatible types
found : java.lang.String
required: java.lang.StringBuffer
代码:
message_data = "LU62XCE0513: Request File I/O Exception =" ;
我定义了 message_data as:
static StringBuffer message_data = new StringBuffer();
那么为什么我不能将一个简单的文字 String
放入标记为 message_data
的 StringBuffer
中? 我的意思是两者都是 String
类型..对吗?
I'm a Java newbie, but I am an experienced programmer (Assembler, COBOL, C/C++, REXX)
I can't understand why the compiler is giving me the following:
[loading java\lang\Throwable.class(java\lang:Throwable.class)]
src\LU62XnsCvr.java:265: incompatible types
found : java.lang.String
required: java.lang.StringBuffer
Code:
message_data = "LU62XCE0513: Request File I/O Exception =" ;
I've defined message_data
as:
static StringBuffer message_data = new StringBuffer();
So why can't I place a simple literal String
into the StringBuffer
labeled message_data
?
I mean BOTH are of type String
.. right ??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不,
String
和StringBuffer
完全不相关。尝试一下:请注意,
StringBuffer
是一个遗留类,最好在新代码中用StringBuilder
替换,除非您需要共享对象(不太可能发生)跨线程。它是源兼容的,所以只需更改名称就可以了。No,
String
andStringBuffer
are totally unrelated. TryWhile we're at it: note that
StringBuffer
is a legacy class, best replaced withStringBuilder
in new code, except in the unlikely case that you need to share the object across threads. It's source compatible, so just change the name and you're fine.两个对象都可以表示一个字符串,但从技术上来说它们并不是都是字符串类型。您可以通过 new StringBuffer(message_data) 直接从字符串创建字符串缓冲区。
Both objects may represent a string but they are thechnically not both of type string. You can create a stringbuffer from a string directly via
new StringBuffer(message_data)
.只需在将消息传递给
Throwable
时调用message_data.toString()
Just call
message_data.toString()
when passing the message to theThrowable