Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java 1.5 StringBuffer/StringBuilder

 
Tom Johnson
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have recently started using JDK1.5. I used to have code like the following :


This works fine in JDK1.4. However, when this is recompiled with 1.5.0_06, i get a compile time error at line **
"Type mismatch: cannot convert from AbstractStringBuilder to StringBuffer"

This is unexpected as the append method returns a StringBuffer object, not AbstractStringBuilder object.
Also, if you pass in a char ('c'), i get a compiler error "Cannot convert from Appendable to StringBuffer", even though again the append(), method returns a StringBuffer object.

Has anyone seen this before or can anyone explain why the method is returning a different object to that which its signature defined.

Cheers

/Tom
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you using an IDE? I tried compiling your code at the command line with 1.5.0_02 and didn't get a compile-time error.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm wondering why you just don't write:

listChanges.read(reader, null);
 
Tom Johnson
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am using the XDE IDE. Sorry I shouldnt have included the listChanges() bit, im just interested in the line **.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is listChanges a JTextArea?
 
Tom Johnson
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No listChanges() is another method, it is irrelevant really for the compilation error im wondering about.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Tom Johnson:
No listChanges() is another method, it is irrelevant really for the compilation error im wondering about.


listChanges sure looks like an object in your code:

Anyway, I would rewrite that as:

Although if you are trying to read into a text component, the JTextComponent read() method is the way to go.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic