stringbuilder

Why StringBuffer has a toStringCache while StringBuilder not?

∥☆過路亽.° 提交于 2019-12-07 02:28:47
问题 In JDK 8, StringBuffer class has a toStringCache , while StringBuilder doesn't. /** * A cache of the last value returned by toString. Cleared * whenever the StringBuffer is modified. */ private transient char[] toStringCache; But why? One possible reason I can think of is that StringBuffer is already synchronized so a cache can be implemented easier. Or maybe historically StringBuffer was implemented this way so old code depends heavily on this feature? Given modern JVM with escape analysis

Understanding of .NET internal StringBuilderCache class configuration

岁酱吖の 提交于 2019-12-06 23:47:46
问题 When I was looking at decompiled .NET assemblies to see some internals, I've noticed interesting StringBuilderCache class used by multiple framework's methods: internal static class StringBuilderCache { [ThreadStatic] private static StringBuilder CachedInstance; private const int MAX_BUILDER_SIZE = 360; public static StringBuilder Acquire(int capacity = 16) { if (capacity <= 360) { StringBuilder cachedInstance = StringBuilderCache.CachedInstance; if (cachedInstance != null && capacity <=

When should you explicitly use a StringBuilder? [duplicate]

本小妞迷上赌 提交于 2019-12-06 16:52:33
问题 This question already has answers here : StringBuilder vs String concatenation in toString() in Java (18 answers) String concatenation in Java - when to use +, StringBuilder and concat [duplicate] (9 answers) Closed 5 years ago . As I understand it, when I do String baz = "foo" + "bar" + "123" the Java compiler internally replaces the expression with a StringBuilder . However our Java teacher told us that it is good practice to always use a StringBuilder explicitly... Am I correct in assuming

String和StringBuffer,StringBuilder 的区别是什么?

吃可爱长大的小学妹 提交于 2019-12-06 11:02:31
1.可变性   String类中使用final关键字,字符数组保存字符串,private final char value[]; ,使用String对象是不可变的。   StringBuffer和StringBuider都是继承 AbstractStringBuilder类,在AbstractStringBuilder中也是使用字符数组保存字符串 char[] value,但是没有用final关键字修饰,所有这里个对象都是可变的。     StringBuilder 与 StringBuffer 的构造方法都是调用父类构造方法也就是 AbstractStringBuilder 实现的,可查看源码。 abstract class AbstractStringBuilder implements Appendable, CharSequence { /** * The value is used for character storage. */ char[] value; /** * The count is the number of characters used. */ int count; /** * This no-arg constructor is necessary for serialization of subclasses. */

java基础

早过忘川 提交于 2019-12-06 10:45:19
浅谈对String,StringBuilder,StringBuffer的认识 String类是final修饰的类,final修饰的类不能够被继承。String类是字符串类,常常被用来进行字符串的操作,但是String的值是不可变的,这就导致每次对String的操作都会产生新的对象,效率低下,而且浪费了大量的内存空间。 由于对字符串的操作产生了大量的对象,浪费空间,为了解决这种状况StringBuilder应运而生,StringBuilder是可变字符串,能够解决字符串操作浪费空间的问题,并且不产生新的对象。但是StringBuilder线程不安全。 为了解决线程安全的问题StringBuffer应运而生,StringBuffer之所有线程安全是因为StringBuffer几乎所有的方法都加入了Synchronized,但是正是因为加入了Synchronized同步锁,使得速度速度变慢,而StringBuilder没有同步锁,所以它的访问速度要比StringBuffer快。 String:使用与少量字符串操作的情况,大量的操作浪费空间,效率低下。 StringBuilder:使用与单线程下在字符缓冲进行大量的操作的情况,它是线程不安全的。 StringBuffer:使用多线程下在字符缓冲区进行大量的操作的情况,它是线程安全的 来源: https://www.cnblogs.com

Is that StringBuilder variable thread safe in this code?

北战南征 提交于 2019-12-06 08:59:36
Consider the below struts Action class in which, I am using a StringBuilder variable inside the execute method. My question: Is the variable sb threadsafe or not? public DemoAction extends Action { ...... public ActionForward execute(.....) { StringBuilder sb = new StringBuilder(); } } What if the same variable sb declared outside the execute(). Remember there will be only one object for DemoAction in WebContainer.? Local variables are thread safe, as long as no other thread somehow gets a reference to the same string builder instance, it’s thread safe. It is threadsafe because you only create

Autofac 小试

陌路散爱 提交于 2019-12-06 08:49:36
ContainerBuilder builder = new ContainerBuilder(); ////builder.RegisterType<GetTest>().As<IBufrTest>(); //builder.RegisterType<GetTest>().AsImplementedInterfaces(); //builder.RegisterType<gettest2>().As<IBufrTest>(); string dllFIle = System.IO.Directory.GetCurrentDirectory() + "\\DemoBufr.dll"; builder.RegisterAssemblyTypes(Assembly.LoadFile(dllFIle)).AsImplementedInterfaces(); IContainer resorlver = builder.Build(); var test = resorlver.Resolve<IEnumerable<IBufrTest>>(); StringBuilder sbtext = new StringBuilder(); foreach (var item in test) { sbtext.Append(item.getstring()).Append("\r\n"); }

Is it true that StringBuilder is slower than concatenate a dozen of strings?

懵懂的女人 提交于 2019-12-06 04:13:16
Is it true that StringBuilder is slower than concatenate a dozen of strings? How does compiler optimize the string concatenation so that joining a dozen of strings using "+" will be better than StringBuilder? From a book (written by Ben Watson) it says that: String Concatenation: For simple concatenation of a known (at compile time) quantity of strings, just use the ‘+’ operator or the String.Concat method. This is usually more efficient than using a StringBuilder. string result = a + b + c + d + e + f; Do not consider StringBuilder until the number of strings is variable and likely larger

String concatenation with the + symbol

时光总嘲笑我的痴心妄想 提交于 2019-12-06 03:43:36
Today I was reading Antonio's Blog about toString() performance and there is a paragraph: What used to be considered evil yesterday (“do not concatenate Strings with + !!!“), has become cool and efficient! Today the JVM compiles the + symbol into a string builder (in most cases) . So, do not hesitate, use it. Now I am confused, because he is saying Today the JVM compiles the + symbol into a string builder (in most cases) , but I have never heard or seen(code) anything like this before. Could someone please give example where JVM does this and in what conditions it happens ? The rule “do not

Where are strings more useful than a StringBuilder?

不羁岁月 提交于 2019-12-06 03:17:28
问题 Lot of questions has been already asked about the differences between string and string builder and most of the people suggest that string builder is faster than string. I am curious to know if string builder is too good so why string is there? Moreover, can some body give me an example where string will be more usefull than string builder? 回答1: It's not a case of which is more useful... A String is a String - one or more characters next to eachother. If you want to change a string in someway