stringbuilder

为什么Java只有值传递?

匿名 (未验证) 提交于 2019-12-02 21:45:52
形式参数,是在方法定义阶段,是定义某个函数时使用的参数,用于接收实参传入。例f(x,y)中x和y是形参。 实际参数,是在方法调用阶段,是主调函数调用有参函数时,实际传递的内容。例f(3,7)中3和7是实参。 值传递和引用传递不是简单地通过传递内容区分的。如果是值,就是值传递;如果是引用,就是引用传递。这一理解是不正确的。 值传递,是指在调用函数时将实际参数复制一份传递给函数形参。此时,在函数中对形参做修改,不影响实际参数。 引用传递,是指在调用函数时将实际参数的地址直接传递给函数形参。此时,在函数中对参数做修改,将影响实际参数。 根本区别 在于值传递会创建副本,因此函数中无法改变原始对象;引用传递不创建副本,函数中可以改变原始对象。 public class ParamPassing { private static int intStatic = 222; private static String stringStatic = "old string"; private static StringBuilder stringBuilderStatic = new StringBuilder("old stringBuilder"); public static void main(String[] args) { // 方法调用1 method(intStatic);

Java 数组和集合list [array,arrayList,linkedList]的效率, 几种for循环[for,for each, lambda] 使用JProfiler进行效率测试

匿名 (未验证) 提交于 2019-12-02 21:40:30
版权声明:觉得此文有用的,不嫌麻烦的,就留个言呐,或者点个赞呐(额,就是文章底部的“顶”啦),要是嫌弃麻烦呢,也麻烦点个赞嘛,要是实在不想点赞呢,也不是不可以。 但是,你要是想踩一脚呢,那还是赶紧,马上,快快的闪人。 小心我手里三十米长的大刀。 哼哼。想想都怕 !!! https://blog.csdn.net/qq_27093465/article/details/91890824 在做效率测试的时候,会发现,在代码里面不起眼的一个for循环,会损失掉不少时间。几种for循环要使用哪个,会效率高点,常用的集合一般都是arrayList,他的循环遍历的效率如何,是不是首选的呢? 开局一张图 然后看这个图的运行代码 package com.lxk.commonTest; import com.lxk.util.CollectionUtil; import java.util.List; /** * 关于for循环的测试 * * @author lxk on 2017/4/21 */ public class ForTest { private static final int SIZE = 40000; public static void main(String[] args) { testForEfficiency(); } /** * 测试不同for的效率问题 */

java中路径符号“//”,\"\\\"转换

匿名 (未验证) 提交于 2019-12-02 21:40:30
之前遇到过"/",在esclipse中报错,只认识“//”,“\”符号,需要将string字符串“/”,转换成“//”或者“\”怎么转呢? 获取字符串是 String path = "/root/data/image"; StringBuilder sb = new StringBuilder(); if (path.contains("\\")) {   String t = path.replaceAll("\\\\", "/");   sb.append(t); } String fromFile = sb.toString(); System.out.println(fromFile); 这样就可以转换成功了! 文章来源: java中路径符号“//”,\"\\\"转换

Dumping a Java StringBuilder to File

匆匆过客 提交于 2019-12-02 20:09:50
What is the most efficient/elegant way to dump a StringBuilder to a text file? You can do: outputStream.write(stringBuilder.toString().getBytes()); But is this efficient for a very long file? Is there a better way? Kevin Bourrillion As pointed out by others, use a Writer, and use a BufferedWriter, but then don't call writer.write(stringBuilder.toString()); instead just writer.append(stringBuilder); . EDIT: But, I see that you accepted a different answer because it was a one-liner. But that solution has two problems: it doesn't accept a java.nio.Charset . BAD. You should always specify a

StringBuilder.Append Vs StringBuilder.AppendFormat

折月煮酒 提交于 2019-12-02 19:58:48
I was wondering about StringBuilder and I've got a question that I was hoping the community would be able to explain. Let's just forget about code readability, which of these is faster and why? StringBuilder.Append : StringBuilder sb = new StringBuilder(); sb.Append(string1); sb.Append("----"); sb.Append(string2); StringBuilder.AppendFormat : StringBuilder sb = new StringBuilder(); sb.AppendFormat("{0}----{1}",string1,string2); casperOne It's impossible to say, not knowing the size of string1 and string2 . With the call to AppendFormat , it will preallocate the buffer just once given the

C# string和StringBuilder区别

南楼画角 提交于 2019-12-02 16:59:55
目录 1、string和StringBuilder的区别 2、代码具体分析 参考博客请看 参考博客 1、string和StringBuilder的区别 string创建后分配在栈区,大小不可修改,每次使用string类中的方法时,都要在内存中创建一个新的字符串对象,就需要再分配新的空间。所以有可能产生很大的开销。 StringBuilder创建后分配在堆区,大小可自由修改。 String a1 = "abc";  //分配固定的内存大小 a1+="def";  //创建新的内存分配a1,代价比较昂贵 StringBuilder sb = new StringBuilder(20);  //指定分配大小。超出容量的话,会自动增倍容量。 sb.Append('abc');  //分配到堆区 sb.Append('def');  //不会被销毁,而是直接追加到后面。 sb.Capacity = 25; //可以使用读/写Capacity属性来设置对象的最大长度。 2、代码具体分析 值类型是存储在内存中的栈,而引用类型的在栈中存储引用类型变量的地址,其本身存储在堆中。 1、==它是比较的栈里面的值是否相等(值比较) 对于值类型,如果对象的值相等,则相等运算符 (==) 返回 true,否则返回 false。 对于引用型变量表示的是两个变量在堆中存储的地址是否相同,即栈中的内容是否相同。 2

Best way to remove the last character from a string built with stringbuilder

爷,独闯天下 提交于 2019-12-02 16:27:49
I have the following data.AppendFormat("{0},",dataToAppend); The problem with this is that I am using it in a loop and there will be a trialling comma. What is the best way to remove the trailing comma? Do I have to change data to a string the substring it? The simplest and most efficient way is to perform this command: data.Length--; by doing this you move the pointer (i.e. last index) back one character but you don't change the mutability of the object. In fact, clearing a StringBuilder is best done with Length as well (but do actually use the Clear() method for clarity instead because that

C# sha256加密算法

南楼画角 提交于 2019-12-02 15:24:32
加密eg: public string sha256(string data) { byte[] bytes = Encoding.UTF8.GetBytes(data); byte[] hash = SHA256Managed.Create().ComputeHash(bytes); StringBuilder builder = new StringBuilder(); for (int i = 0; i < hash.Length; i++) { builder.Append(hash[i].ToString("X2")); } return builder.ToString(); }    来源: https://www.cnblogs.com/chenze-Index/p/11751698.html

How is StringBuffer implementing append function without creating two objects?

感情迁移 提交于 2019-12-02 15:15:48
It was an interview question. I was asked to implement the StringBuffer append function. I saw the code after the interview. But I cannot understand how the operation is done with creation of a single object. I am thinking like this. String s = "orange"; s.append("apple"); Here two objects are created. But StringBuilder s = new StringBuilder("Orange"); s.append("apple"); Now here only one object is created. How is Java doing this operation? First there is a problem with your question: String s = "orange"; s.append("apple"); here two objects are created Correct, two Objects are created, the