This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
The moose likes Java in General and the fly likes What is the reason ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "What is the reason ?" Watch "What is the reason ?" New topic

What is the reason ?

deepak kukreja
Ranch Hand

Joined: Aug 13, 2001
Posts: 54
We have two classes namely String and StringBuffer.
If we can do everything with StringBuffer, then what is the
need for String class.

Deepak<br />SCJP
Aaron Roberts
Ranch Hand

Joined: Sep 10, 2002
Posts: 174
They are for different purposes. A StringBuffer is used when you want to insert/remove/splice/etc characters on the fly, something Strings can't do. A StringBuffer might be loosely thought of as an ArrayList of chars, since it can grow and shrink dynamically. A String can not change its size.
Joel McNary

Joined: Aug 20, 2001
Posts: 1824

The String class is there for speed/performance. It does not carry the same overhead as a StringBuffer object.

Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
I agree. Here's the link:
subject: What is the reason ?
jQuery in Action, 3rd edition