• Post Reply Bookmark Topic Watch Topic
  • New Topic

What is the advantage of using String over StringBuffer?  RSS feed

 
Biswajit Paria
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
Could you please tell me,what is the advantage of using String over StringBuffer?

Regards,
Biswajit Paria
 
Sharath Kumar
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the advantage of String over StringBuffer is once the value(contents) assigned to a String instances cannot be altered but whereas the contents of the StringBuffer can be changed. and the size of the String cannot be changed once declared.


Correct me if i am wrong.
 
Biswajit Paria
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank Sharath Kumar for your reply.

I think what you have told is the behaviour of string but i want to know the advantage of using String over String Buffer.

The thing you have mentioned is that we can't change the String value but that can be achieved by another way of using 'final' keywork to any variable!

Hi All,
Please throw some light on this topic please :roll:

Regards,
Biswajit.
 
Shaan Shar
Ranch Hand
Posts: 1249
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Biswajit Paria:

The thing you have mentioned is that we can't change the String value but that can be achieved by another way of using 'final' keywork to any variable!


Are you sure Mr Paria, that using final keyword we cann't change the value...

Let me know if this is verified...
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Biswajit Paria:
Thank Sharath Kumar for your reply.

I think what you have told is the behaviour of string but i want to know the advantage of using String over String Buffer.

The thing you have mentioned is that we can't change the String value but that can be achieved by another way of using 'final' keywork to any variable!

Hi All,
Please throw some light on this topic please :roll:

Regards,
Biswajit.


You are right, this is the behaviour of string, not the advantage.

The advantage is, it is less time consuming to create String objects compare to StringBuffer and so use StringBuffer only if it is needed...
 
Biswajit Paria
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes we can't reassign any value to final variable again!
Is it ok, Mr. Ankur Sharma ?

Ok,Let's move to Originl question....
What is the advantage of using String over String Buffer?

One advantage I have recieved from my friend is that of overloaded '+' operator we can use for String and it is very handy for concatenating :-)

Regards,
Biswajit.
 
Shaan Shar
Ranch Hand
Posts: 1249
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Biswajit Paria:
yes we can't reassign any value to final variable again!
Is it ok, Mr. Ankur Sharma ?

Ok,Let's move to Originl question....
What is the advantage of using String over String Buffer?

One advantage I have recieved from my friend is that of overloaded '+' operator we can use for String and it is very handy for concatenating :-)

Regards,
Biswajit.


Well,

I must currect you Mr Paria, we cann't change the reference of final variables but we can change the value of final variable.

Please let us know, if you got this point. And if you have any concern then revert with your kind concern.
 
Biswajit Paria
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ankur Sharma for correcting me!

Could you please look into that issue what I am asking?

Regards,
Biswajit.
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the bit where EFH requests that users don't mislead those who are seeking advice with prolific amounts of misinformation, right?
 
Srinivas Kalvala
Ranch Hand
Posts: 257
Firefox Browser Hibernate Linux
 
Shaan Shar
Ranch Hand
Posts: 1249
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Biswajit Paria:
Thanks Ankur Sharma for correcting me!

Could you please look into that issue what I am asking?

Regards,
Biswajit.


Well Tony,

I was not intended to mislead the topic...

Anyways the Advantages of Using String over StringBuffer are:

1. Less memory used in the stack for the String. Wouldn't have to use the toString method when you wanted to access the values.

2. JVM uses a uses a pool in a memory for strings. If you have string of same value then you will have only one object of that value will be present in the memory an both the reference objects will be poiting to the same object in the pool. If you change the value of string object, JVM basically create a new string object in the pool and the reference of this object will be stored in the given reference pointer. the old string object will be collected by Garbage collector if you don't have any reference object pointing to it.

StringBuffers are normal java objects. When you call replace() on it it actually changes the value of the same object where as this does not happen in the String mechanism.

3. Strings take advantage of the single overloaded operator in Java, +, for concatenation.
[ September 11, 2006: Message edited by: Ankur Sharma ]
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Tony Morris:
This is the bit where EFH requests that users don't mislead those who are seeking advice with prolific amounts of misinformation, right?




Let's see. I do make that speech sometimes. In this case, I won't because the original question doesn't have any good answer. It's like asking "What's the advantage of using a doughnut over a pipe wrench?" They each have their purpose, and neither is appropriate for the uses to which the other can and should be put. It's a moot point, really, as most of the time, Strings are used when interacting with existing APIs, both Java's official APIs and third party ones, and so of course you don't have any choice there anyway.

Mr. Sharma, your last post says you are not intending to mislead, but then you follow that with a lot of misinformation. For example,

-- a String, a StringBuffer, an int, a float, and a JButton all take precise the same amount of memory "on the stack." Their heap usages are not all the same, although the heap size difference between a String and a StringBuffer is tiny if there's a difference at all.

-- only literal Strings are held in the String pool, unless you explicitly place other Strings there with the intern() method.

-- there is no "magic" at all of the kind you describe here -- the JVM does not, in any way, shape, or form, detect an attempt to modify a String and automatically cerate a new one. String objects are simply not changeable; it's the plain, ordinary Java code in String's methods that always return new Strings rather than modifying the existing one.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ernest Friedman-Hill:
there is no "magic" at all of the kind you describe here -- the JVM does not, in any way, shape, or form, detect an attempt to modify a String and automatically cerate a new one. String objects are simply not changeable; it's the plain, ordinary Java code in String's methods that always return new Strings rather than modifying the existing one.


Well, there is a little bit of magic in the *compiler*, that replaces string concatenation operators with calls to appropriate methods of StringBuilder or similar.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is a good description of the advantages of immutable objects, which Strings are just an example of: http://www.javapractices.com/Topic29.cjp
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As to why use String instead of StringBuffer ... just list out the methods on each and see if String has any that StringBuffer doesn't. If you need one of those, you need String. No?
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Tony Morris:
This is the bit where EFH requests that users don't mislead those who are seeking advice with prolific amounts of misinformation, right?


I was thinking exactly the same thing about the time I got to your reply.
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Stan James:
As to why use String instead of StringBuffer ... just list out the methods on each and see if String has any that StringBuffer doesn't. If you need one of those, you need String. No?


I think, the comment should be reverse, I mean, it could be true for StringBuffer... usually we use String.
:roll:
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!