1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > Java 集合 --- String StringBuilder StringBuffer

Java 集合 --- String StringBuilder StringBuffer

时间:2022-08-17 19:21:44

相关推荐

Java 集合 --- String  StringBuilder  StringBuffer

Java 集合 --- String, StringBuilder, StringBuffer

StringString的声明方式String的不可变性对比两个String StringBuilderStringBuilder 和 StringBuffer 的区别

String

字符串对象,程序中定义""都是字符串对象,这个对象的使用频率最高.字符串类 java.lang.String类,继承Object类,实现了三个接口.

String的声明方式

直接赋值: String s = “hello world” — 储存在常量池调用构造器 String s = new String(“hello world”) ---- 储存在堆中的地址JVM为了提高性能和减少内存开销,在实例化字符串常量的时候进行了一些优化, 为字符串开辟一个字符串常量池,类似于缓存区
直接赋值时,首先坚持字符串常量池是否存在该字符串. 所以如果再次创建一个 String ss = "“hello world”. 将会和 s 指向同样的字符串常量

String的不可变性

String类声明为final, 所以不能被继承String类实际储存字符串数据的是类内部的成员变量final char [] value
因为value被final修饰, 所以value不能指向新的地址也就是不能再引用其他String变量
String 内部没有改变 value 数组的方法,也就是不能更改String中任何一个char 元素, 因此可以保证 String 不可变。

String不可变性的体现:只要对string做出修改, 就会开辟新的内存空间

1.当对字符串重新赋值时,会重新开辟新的内存空间储存新的字符串

//常量池中会同时储存 "hello world" 和 "hello" , 而不是 "hello world" 被覆盖掉String s = "hello world"s = "hello"

public static void main(String args[]) {String s = "hello";String temp = s;s = "world"; String ss = "hello"; if (ss == temp) {//注意这里对比的是地址, 说明ss和temp指向同样的地址//也就是 "hello" 没有被world覆盖掉System.out.println("same");}}output: "same"

2.当对现有的字符串进行连接操作时,会重新开辟新的内存空间储存拼接后的字符串3.当调用String的replace().或者substring()等方法修改指定字符或字符串时,也会重新开辟新的内存空间储存修改后的字符串

public static void main(String args[]) {String s = "hello";String temp1 = s;s = s + "world" ;String temp2 = s;if (temp1 != temp2) {System.out.println("not same");}}output: "not same"

对比两个String

使用 == 对比两字符串时实际对比的是地址, 而不是实际内容所以一律使用s1.equals(s2)或者pareTo(s2)对比string

public static void main(String args[]) {String s = "hello";String ss = "hello";String sss = new String("hello");System.out.println(s == ss);System.out.println(s == sss);System.out.println(ss == sss);}output:true // 因为 直接赋值创建String时, JVM会首先在常量池中寻找是否已经有同样的字符串. 所以s和ss指向同样的地址false //使用构造器创建String时, 地址会指向堆内存, 然后堆内存中的地址指向常量池, 所以不一样false //同上

Another example:

public class Solution {static public String fun1(String s1) {return s1;}public static void main(String args[]) {String s = "hello";String ss = fun1(s);//因为s和ss都等于hello, 所以共用常量池中的字符串System.out.println(s == ss);}}ouput: true//但是如果在fun1中对s1进行任何修改, 如拼接等, 就会开辟新的内存空间, 则 s != ss

Another Example

public static void main(String[] args) {String s = "Hello";String ss = s;//此时ss和s指向同一个地址s = "World";//常量池中开辟了新的空间, s指向新的地址if (ss == s) {System.out.println("same");}else {System.out.println("not same");}System.out.println(s.hashCode());System.out.println(ss.hashCode());//s和ss指向不一样的地址, 所以not same}

Another Example

static public void fun1(String s1) {String newS = "hello";System.out.println(newS == s1);//答案是相等}public static void main(String args[]) {String s = "hello";fun1(s);}

StringBuilder

如果代码中需要对string进行大量修改, 则推荐使用 StringBuilder类StringBuilder 类的对象能够被多次的修改,并且不产生新的未使用对象

Example:

public void pathToStartValue(TreeNode node, int startValue, String pathStart) {if (node == null) {return;}if (node.val == startValue) {pathTostartValue = pathStart;return;}//这里pathStart + "U" 会生成新的String, 最后导致内存超限//所以需要改用StringBuilderpathToStartValue(node.left, startValue, pathStart + "U");pathToStartValue(node.right, startValue, pathStart + "U");return;}

StringBuilder提供对String的增删改查, 包括:

append:

insert

setCharAt

void setCharAt(int index, char ch)//The character at the specified index is set to ch.

delete

StringBuilderdelete(int start, int end)//Removes the characters in a substring of this sequence.StringBuilderdeleteCharAt(int index)//Removes the char at the specified position in this sequence.

replace

StringBuilderreplace(int start, int end, String str)//Replaces the characters in a substring of this sequence with characters in the specified String.

substring

Stringsubstring(int start)//Returns a new String that contains a subsequence of characters currently contained in this character sequence.Stringsubstring(int start, int end)//Returns a new String that contains a subsequence of characters currently contained in this sequence.

StringBuilder 和 StringBuffer 的区别

StringBuilder 类在 Java 5 中被提出,它和 StringBuffer 之间的最大不同在于 StringBuilder 的方法不是线程安全的(不能同步访问)。
由于 StringBuilder 相较于 StringBuffer 有速度优势,所以多数情况下建议使用 StringBuilder 类。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。