tag:blogger.com,1999:blog-8712770457197348465.post2643839187184124055..comments2024-03-29T03:17:04.645-07:00Comments on Javarevisited: String vs StringBuffer vs StringBuilder in Java? Examplejavin paulhttp://www.blogger.com/profile/15028902221295732276noreply@blogger.comBlogger28125tag:blogger.com,1999:blog-8712770457197348465.post-60285756256507858062017-06-04T07:14:07.642-07:002017-06-04T07:14:07.642-07:00"... his hashcode value can be cached which m..."... his hashcode value can be cached which makes it a faster hashmap key".<br /><br />... unless the hashCode value happens to be 0, in which case it is always recalculated and will be unexpectedly slow.<br /><br />Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-61770522750724262372017-01-02T00:14:26.487-08:002017-01-02T00:14:26.487-08:00Use String if you need Immutable and thread safe. ...Use String if you need Immutable and thread safe. As any Immutable object is a thread safe. So as a conclusion you want a thread safe class use String (Immutable) or StrinhBuffee (muttable). You want a non thread safe for a faster manipulation use the StringBuilder (Immutable of course).Houssemhttps://www.blogger.com/profile/01703774059228916603noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-85496128911086192052016-09-08T06:55:15.242-07:002016-09-08T06:55:15.242-07:00@Anonymous, interesting question. My answer is 5, ...@Anonymous, interesting question. My answer is 5, let me know if there is a trick :-). The 3 String objects are right there, then one StringBuffer or StringBuilder depending upon which Java version you are using for concatenation and finally one more String which will be stored in d, and returned as StringBuffer.toString() method. javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-4756542806449083522016-09-06T16:20:12.171-07:002016-09-06T16:20:12.171-07:00So i was interviewing candidates for my company in...So i was interviewing candidates for my company in McLean-VA and asked this and too bad none of the candidates know the answers my question-<br />String a = "A";<br />String b = "B";<br />String c = "C";<br /><br />Question: String d = a + a + b + c;<br />All in all in the above question how many objects have you created? I leave to the candidates to answer again..Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-84599558775475462502015-08-07T04:20:20.575-07:002015-08-07T04:20:20.575-07:00Is there any possibility to loose the tail of a HU...Is there any possibility to loose the tail of a HUGE stringBuilder if toString() method is called on it get string?A.M.S Sumanasooriyahttps://www.blogger.com/profile/02411961135312131621noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-15133060768860189282015-01-04T01:50:48.097-08:002015-01-04T01:50:48.097-08:00It's worth noting that StringBuffer was replac...It's worth noting that StringBuffer was replaced by StringBuilder ten years ago. There is almost never a good reason to use StringBuffer as it is only thread safe for single operations. If you consider that using StringBuffer for not more than one operation is pretty useless, you can see why StringBuffer was rather pointless.Peter Lawreyhttps://www.blogger.com/profile/17982030676088168612noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-42292579543942406282014-12-01T07:27:08.047-08:002014-12-01T07:27:08.047-08:00What happens in heap if we called toString() on St...What happens in heap if we called toString() on StringBuffer or StringBuilder's object ?<br /><br />This question is asked to me in an Interview.<br />can any one help me to understand what exactly will happen in heap.<br /> Anonymoushttps://www.blogger.com/profile/16750749408361996726noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-76814444030009241822014-02-16T22:59:10.976-08:002014-02-16T22:59:10.976-08:00Hi, How we can make stringbuffer behave like strin...Hi, How we can make stringbuffer behave like string?Nidhihttps://www.blogger.com/profile/16196198954142025495noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-44947063725547494812013-09-16T02:18:15.177-07:002013-09-16T02:18:15.177-07:00once i was asked in an interview that...how we can...once i was asked in an interview that...how we can made stringbuffer behave like string...i.e make string buffer immutable...Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-1928581157533610572013-08-02T22:31:31.950-07:002013-08-02T22:31:31.950-07:00@Milind, That's a good point. Also if you have...@Milind, That's a good point. Also if you have noticed that, while applying trim(), toUpperCase() or toLowerCase() method on String, you better store the result of this operation, failing to do so will result in subtle bugs e.g.<br /><br />String a = "abc";<br />a.toUpperCase();<br />System.out.println(a); // will still print "abc" <br /><br />Correct way of doing this is<Javin @ Java String Tutorial for Beginnershttp://javarevisited.blogspot.com/2013/07/java-string-tutorial-and-examples-beginners-programming.htmlnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-61024141655215156272013-08-01T00:09:01.568-07:002013-08-01T00:09:01.568-07:00To prove/understand clearly that String is immutab...To prove/understand clearly that String is immutable (and creates saperate objects in memory) and StringBuffer/StringBuilder are not you can observe this- Every manipulation method in string like concate, replace returns you new Sring and original string objects remain unchanged, while same kind of methods in buffer and builder are void and actual object gets changed after these methods.Anonymoushttps://www.blogger.com/profile/17614192764988139343noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-50831406244330075182013-05-02T02:12:02.769-07:002013-05-02T02:12:02.769-07:00Vinay, == operator compares the address when compa...Vinay, == operator compares the address when comparing objects and value when comparing primitive data typesAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-3023913950935390752013-04-05T04:15:08.600-07:002013-04-05T04:15:08.600-07:00why we can not use == operator to compare String a...why we can not use == operator to compare String and StringBuffer object while we can use equal() method to compare String and StringBuffer object?Anonymoushttps://www.blogger.com/profile/16013789706810922285noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-12396957357598391662012-06-24T08:59:57.425-07:002012-06-24T08:59:57.425-07:00This is nice article. Thank you so much... I knew ...This is nice article. Thank you so much... I knew how to String, StringBuffer and StringBuider work. :dAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-3643978193015572222012-06-11T11:33:00.650-07:002012-06-11T11:33:00.650-07:00http://ilovemeturtle.blogspot.com/2012/06/string-v...http://ilovemeturtle.blogspot.com/2012/06/string-vs-stringbuilder-vs-stringbuffer.htmlAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-28814970230893678782012-06-01T08:16:29.037-07:002012-06-01T08:16:29.037-07:00Sorry U Post things know by all already can you Ex...Sorry U Post things know by all already can you Explain in depthAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-26465129479920648642012-04-17T06:58:39.358-07:002012-04-17T06:58:39.358-07:00@Anonymous, Good to know that you like Difference ...@Anonymous, Good to know that you like Difference between StringBuffer vs String and StringBuilder in Java. I would like to thanks people who has shared there knowledge via comment as well. Look forward to see you again.Javin @ LinkedList vs ArrayList Javahttp://javarevisited.blogspot.com/2012/02/difference-between-linkedlist-vs.htmlnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-78533528861013342842012-04-17T03:42:23.255-07:002012-04-17T03:42:23.255-07:00Quick, useful synapsis. And correct on interview ...Quick, useful synapsis. And correct on interview questions along with what are some immutable classes... Good point made about peformance differences between "+" and .append().Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-46471220135649663972012-03-02T03:45:16.103-08:002012-03-02T03:45:16.103-08:00@Anonymous, Thanks for your comment. first of all ...@Anonymous, Thanks for your comment. first of all using "+" for concatenating String is not great in terms of performance as you have pointed out. at that line I was suggesting that internally + is implemented using StringBuilder but it doesn't mean that you should use + for excessive String manipulation,that's most simplest way and should be used for simple reason. having a Javin @ arraylist sort ascending order javahttp://javarevisited.blogspot.com/2012/01/how-to-sort-arraylist-in-java-example.htmlnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-65054856479241909742012-03-01T08:43:17.046-08:002012-03-01T08:43:17.046-08:00So, I would like to point out a slight flaw in the...So, I would like to point out a slight flaw in the article:<br /><br />You said:<br />"You can also use "+" for concatenating two string because "+" operation is internal implemented using either StringBuffer or StringBuilder in Java."<br /><br />So, you are saying there is a scenario where I have two String Objects, lets call them S1 and S2. And I want to combine Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-61966108462890600462012-02-13T05:09:24.275-08:002012-02-13T05:09:24.275-08:00@Anonymous, thanks for your comment and glad to he...@Anonymous, thanks for your comment and glad to hear that you like this article. you may like my other post on String e.g. <a href="http://javarevisited.blogspot.com/2011/10/how-substring-in-java-works.html" rel="nofollow">How SubString works in Java</a> and <a href="http://javarevisited.blogspot.com/2011/09/step-by-step-guide-to-convert-string-to.html" rel="nofollow">How to convert String to Javin @ sort array in javahttp://javarevisited.blogspot.com/2012/01/sort-array-in-java-ascending-and.htmlnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-84341020077412089342011-12-14T03:39:07.439-08:002011-12-14T03:39:07.439-08:00Thanks for your comment Sandeep. Indeed Stringbuff...Thanks for your comment Sandeep. Indeed Stringbuffer and StringBuilder are same with only difference in synchronization part.Javin @ String split Javahttp://javarevisited.blogspot.com/2011/09/string-split-example-in-java-tutorial.htmlnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-33068406018876535992011-11-30T21:38:15.114-08:002011-11-30T21:38:15.114-08:00If + is implemented through StringBuilder or Strin...If + is implemented through StringBuilder or StringBuffer internally, is it as performance draining, when we use a lot "+"s when doing System.out.println()?Jennyhttps://www.blogger.com/profile/08769473433598684630noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-67949797922126216802011-08-17T06:06:54.030-07:002011-08-17T06:06:54.030-07:00Hi Anonymous, good catch, A String object represen...Hi Anonymous, good catch, A String object represents a string in the UTF-16 format in which supplementary characters are represented by surrogate pairs. so in essence point 4 and 8 is same.Javin @ volatile keyword in javahttp://javarevisited.blogspot.com/2011/06/volatile-keyword-java-example-tutorial.htmlnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-21143151288714406222011-08-16T08:31:56.605-07:002011-08-16T08:31:56.605-07:00In point 4 you say that String is backed by a UTF-...In point 4 you say that String is backed by a UTF-8 Character Array but in point 8 you say it's represented in UTF-16 !<br />Which one is correct ?Anonymousnoreply@blogger.com