File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why is StringBuilder a modifiable interface variable ?

 
Graeme Byers
Ranch Hand
Posts: 127
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why don't StringBuffer and StringBuilder when declared in interfaces behave as expected ?
Thank you.

 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java has no concept of making a particular instance of a class "unmodifiable." If you use an instance of a modifiable (usually we say "mutable") class as a member of an interface, then it's going to be possible to modify that object.

Note that in your example, you didn't show something like

BUF = new StringBuffer("replacement");

If you tried this, you'd find that because BUF is final, this is illegal. The object that BUF points to can be modified, but BUF, being final, can not.

Make sense?
 
Vikas Kapoor
Ranch Hand
Posts: 1374
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And We all know that STRING IS IMMUTABLE.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic