stringbuilder

String拼接字符串效率低,你知道原因吗?

假装没事ソ 提交于 2019-12-15 19:00:44
为什么String用"+"拼接字符串效率低下,最好能从JVM角度谈谈吗? 对于这个问题,我们先来看看如下代码: public class StringTest { public static void main ( String [ ] args ) { String a = "abc" ; String b = "def" ; String c = a + b ; String d = "abc" + "def" ; System.out.Println ( c ) ; System.out.Println ( d ) ; } } 打印结果: abcdef abcdef 从上面代码示例中,我们看到两种方式拼接的字符串打印的结果是一样的。但这只是表面上的,实际内部运行不一样。 两者究竟有什么不一样? String c = a + b; "+"相当于new了一个StringBuilder,然后调用StringBuilder的初始化方法,然后进行append操作,最后toString(); String d = “abc” + “def”; 两个常量在同行发生时,JVM在编译时就认为这个“+”是没有用处的,编译时直接变成String d = “abcdef”; 那么效率问题从何说起? 其实上面两个例子,连接字符串行表达式很简单,那么"+"和StringBuilder基本是一样的

Java中String,StringBuilder和StringBuffer的区别

泄露秘密 提交于 2019-12-15 11:45:40
这三个类的主要区别在两个方面:运算速度(运算性能或执行效率)和线程安全性。 1、运算速度比较(通常情况下):StringBuilder > StringBuffer > String String是final类不能被继承且为字符串常量,而StringBuilder和StringBuffer均为字符串变量。String对象一旦创建便不可更改,而后两者是可更改的,它们只能通过构造函数来建立对象,且对象被建立以后将在内存中分配内存空间,并初始保存一个null,通过append方法向StringBuffer和StringBuilder中赋值。请看如下示例代码: String str = "abc"; System.out.println(str); str = str + "de"; System.out.println(str); 上述代码先创建一个String对象str,并赋值abc给str,然后运行到第三行,JVM会再创建一个新的str对象,并将原有str的值和de加起来再赋值给新的str。而第一个创建的str对象被JVM的垃圾回收机制(GC)回收掉。所以str实际上并没有被更改,即String对象一旦创建就不可更改。所以Java中对String对象进行的操作实际上是一个不断创建并回收对象的过程,因此在运行速度上很慢。

java面试题

痞子三分冷 提交于 2019-12-15 04:40:07
Java面试题(一) 1、面向对象的特征有哪些方面? 2、访问修饰符 public,private,protected,以及不写(默认)时的区别? 3、String 是最基本的数据类型吗? 4、float f=3.4;是否正确? 5、short s1 = 1; s1 = s1 + 1;有错吗?short s1 = 1; s1 += 1;有错吗? 6、Java 有没有 goto? 7、int 和 Integer 有什么区别? 8、&和&&的区别? 9、解释内存中的栈(stack)、堆(heap)和方法区(method area)的用法。 10、Math.round(11.5) 等于多少?Math.round(-11.5)等于多少? 11、switch 是否能作用在 byte 上,是否能作用在 long 上,是否能作用在 String 上? 12、用最有效率的方法计算 2 乘以 8? 13、数组有没有 length()方法?String 有没有 length()方法? 14、在 Java 中,如何跳出当前的多重嵌套循环? 15、构造器(constructor)是否可被重写(override)? 16、两个对象值相同(x.equals(y) == true),但却可有不同的 hashcode,这句话对不对? 17、是否可以继承 String 类? 18

String和StringBuffer和StringBilder的异同区别

*爱你&永不变心* 提交于 2019-12-15 04:16:31
综下所述:String和StringBuffer和StringBilder的区别主要如下: 1、String不可变字符串,StringBuffer和StringBilder为可变字符串。 2、String效率最低,StringBilder效率最高。 3、线程安全上,StringBuffer优于StringBilder 4、初始化上的区别,String可以空赋值,StringBuffer和StringBuilder不行 5、String实现了equals()方法和hashCode()方法,StringBuffer没有实现equals()方法和hashCode()方法。 6、StringBuffer和StringBuilder在修改字符串方面比String的性能要高。 以下是详细解析 String 1、String的值是不可变的,当一个String对象老是被修改的时候,堆内存中需要不断创建新的内存来存储新的值,这会极大地浪费内存。这些无用的对象会被垃圾回收器回收。 2、 StringBuffer 和 StringBuilder 当对字符串进行修改的时候,需要使用 StringBuffer 和 StringBuilder 类。 StringBuffer 和 StringBuilder 能够多次修改而不产生新的未使用的对象。 StringBuilder 类在 Java 5 中被提出,它和

StringBuilder Won't Show In TextBox (WinForms, C#)

牧云@^-^@ 提交于 2019-12-14 03:59:28
问题 I'm trying to get the contents of a StringBuilder to display in a WinForms textbox, but the window never appears when I try to compile the program. This is my first venture into WinForms and C# and I've only used the language for about a week and a half now, so this is probably a simple fix that I'm just not seeing. public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { StringBuilder builder = new StringBuilder();

DllImport, Char*& and StringBuilder C/C#

孤街浪徒 提交于 2019-12-13 15:05:41
问题 I have a problem, I tried to look at almost all the poster solutions, unsuccessful to find a suitable one. The question is easy, Want to have a return string from unmanaged C code in my managed C#. The c function is: extern "C" __declspec(dllexport) int process_batch (char *&result); and in C# I imported the DLL: [DllImport("mydll.dll")] public static extern IntPtr process_batch(StringBuilder result); I run, but the return value in my StringBuilder is a 7-8 character string of non-sense! (I

StringBuffer/StringBuilder

痞子三分冷 提交于 2019-12-13 12:43:07
1、字符串缓冲区\字符串建造者,存放了一个String(char数组),但是可以修改(没有被final修饰) 2、Buffer是线程安全的,Builder是线程不安全的 3、常用方法: 1.构造方法:StringBuilder()/StringBuilder(String str) 2.append(String str):在字符串缓冲区后追加一个字符串 3.toString():将StringBuilder对象转为String对象 4.insert(int offset, String str):在指定索引位置插入字符串 5.其他方法类同String类 6.StringBuffer的方法和StringBuilder()一致 4.注意事项 String的“+”拼接字符串,底层实现是StringBuilder的append方法 1.将字符串作为构造方法参数创建StringBuilder对象 2.调用append方法拼接字符串 3.toString转为字符串 5.最佳实践 在字符串操作频繁的代码中(如:循环内部),建议手动创建StringBuilder类,然后使用StringBuilder的方法操作字符串,最后转为String类的对象 public class Demo01StringBuilder { public static void main ( String [ ] args

In C#, best way to check if stringbuilder contains a substring

耗尽温柔 提交于 2019-12-13 12:26:12
问题 I have an existing StringBuilder object, the code appends some values and a delimiter to it. I want to modify the code to add the logic that before appending the text, it will check if it already exists in the StringBuilder . If it does not, only then will it append the text, otherwise it is ignored. What is the best way to do so? Do I need to change the object to string type? I need the best approach that will not hamper performance. public static string BuildUniqueIDList(context

转载之Java代码优化细节

点点圈 提交于 2019-12-13 10:49:44
可以提高千倍效率的Java代码小技巧 来源:www.cnblogs.com/Qian123/p/6046096.html 前言 代码优化 ,一个很重要的课题。可能有些人觉得没用,一些细小的地方有什么好修改的,改与不改对于代码的运行效率有什么影响呢?这个问题我是这么考虑的,就像大海里面的鲸鱼一样,它吃一条小虾米有用吗?没用,但是,吃的小虾米一多之后,鲸鱼就被喂饱了。 代码优化也是一样,如果项目着眼于尽快无BUG上线,那么此时可以抓大放小,代码的细节可以不精打细磨;但是如果有足够的时间开发、维护代码,这时候就必须考虑每个可以优化的细节了,一个一个细小的优化点累积起来,对于代码的运行效率绝对是有提升的。 代码优化的目标是 1、减小代码的体积 2、提高代码运行的效率 代码优化细节 1、尽量指定类、方法的final修饰符 带有final修饰符的类是不可派生的。在Java核心API中,有许多应用final的例子,例如java.lang.String,整个类都是final的。为类指定final修饰符可以让类不可以被继承,为方法指定final修饰符可以让方法不可以被重写。如果指定了一个类为final,则该类所有的方法都是final的。Java编译器会寻找机会内联所有的final方法,内联对于提升Java运行效率作用重大,具体参见Java运行期优化。 此举能够使性能平均提高50% 。 2

Overwrite file, but only parts that are not spaces

a 夏天 提交于 2019-12-13 06:23:45
问题 I am looking for a way to overwrite certain parts in a .dat file with 00000. For this I have a StringBuilder with content like this: "00000000 0000000000000000 " Now I am looking for a method that overwrites the parts in the file with zeroes, and justs keeps the content of the parts with spaces. So if I have "AUEUIGHEVjerhvgm,eiouhegfwoedjkjjjjjjjjjjjjjjje" I want it to turn into "00000000Vjerhvgm,eio0000000000000000jjjjjjjjjje" Does such a method exist? Or should I accomplish this task in