📜  Java中的String vs StringBuilder vs StringBuffer

📅  最后修改于: 2022-05-13 01:54:40.689000             🧑  作者: Mango

Java中的String vs StringBuilder vs StringBuffer

字符串是一个字符序列。在Java中, String 的对象是不可变的,这意味着一个常量,一旦创建就不能更改。初始化字符串是具有更深入理解的先决条件所需的重要支柱之一。现在,我们将证明让我们考虑下面的代码,其中包含三个连接函数和三种不同类型的参数,String、StringBuffer 和 StringBuilder。让我们通过下面的单个Java程序来澄清它们之间的理解,我们将从生成的输出中得出结论,以找出Java中 String、StringBuilder 和 StringBuffer 之间的差异。

例子

Java
// Java program to demonstrate difference between
// String, StringBuilder and StringBuffer
 
// Main class
class GFG {
 
    // Method 1
    // Concatenates to String
    public static void concat1(String s1)
    {
        s1 = s1 + "forgeeks";
    }
 
    // Method 2
    // Concatenates to StringBuilder
    public static void concat2(StringBuilder s2)
    {
        s2.append("forgeeks");
    }
 
    // Method 3
    // Concatenates to StringBuffer
    public static void concat3(StringBuffer s3)
    {
        s3.append("forgeeks");
    }
 
    // Method 4
    // Main driver method
    public static void main(String[] args)
    {
        // Custom input string
        // String 1
        String s1 = "Geeks";
 
        // Calling above defined method
        concat1(s1);
 
        // s1 is not changed
        System.out.println("String: " + s1);
 
        // String 1
        StringBuilder s2 = new StringBuilder("Geeks");
 
        // Calling above defined method
        concat2(s2);
 
        // s2 is changed
        System.out.println("StringBuilder: " + s2);
 
        // String 3
        StringBuffer s3 = new StringBuffer("Geeks");
 
        // Calling above defined method
        concat3(s3);
 
        // s3 is changed
        System.out.println("StringBuffer: " + s3);
    }
}


Java
// Java program to demonstrate conversion from
// String to StringBuffer and StringBuilder
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Custom input string
        String str = "Geeks";
 
        // Converting String object to StringBuffer object
        // by
        // creating object of StringBuffer class
        StringBuffer sbr = new StringBuffer(str);
 
        // Reversing the string
        sbr.reverse();
 
        // Printing the reversed string
        System.out.println(sbr);
 
        // Converting String object to StringBuilder object
        StringBuilder sbl = new StringBuilder(str);
 
        // Adding it to string using append() method
        sbl.append("ForGeeks");
 
        // Print and display the above appended string
        System.out.println(sbl);
    }
}


Java
// Java Program to Demonstrate Conversion from
// String to StringBuffer and StringBuilder
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Creating objects of StringBuffer class
        StringBuffer sbr = new StringBuffer("Geeks");
        StringBuilder sbdr = new StringBuilder("Hello");
 
        // Converting StringBuffer object to String
        // using toString() method
        String str = sbr.toString();
 
        // Printing the above string
        System.out.println(
            "StringBuffer object to String : ");
        System.out.println(str);
 
        // Converting StringBuilder object to String
        String str1 = sbdr.toString();
 
        // Printing the above string
        System.out.println(
            "StringBuilder object to String : ");
        System.out.println(str1);
 
        // Changing StringBuffer object sbr
        // but String object(str) doesn't change
        sbr.append("ForGeeks");
 
        // Printing the above two strings on console
        System.out.println(sbr);
        System.out.println(str);
    }
}


Java
// Java program to Demonstrate conversion from
// String to StringBuffer and StringBuilder
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Creating object of StringBuffer class and
        // passing our input string to it
        StringBuffer sbr = new StringBuffer("Geeks");
 
        // Storing value StringBuffer object in String and
        // henceforth converting StringBuffer object to
        // StringBuilder class
        String str = sbr.toString();
        StringBuilder sbl = new StringBuilder(str);
 
        // Printing the StringBuilder object on console
        System.out.println(sbl);
    }
}


输出
String: Geeks
StringBuilder: Geeksforgeeks
StringBuffer: Geeksforgeeks

输出说明:

  • Concat1 :在这个方法中,我们传递一个字符串“Geeks”并执行“s1 = s1 +”forgeeks”。从 main() 传递的字符串没有改变,这是因为 String 是不可变的。更改字符串的值会创建另一个对象,并且 concat1() 中的 s1 存储新字符串的引用。 main() 和 cocat1() 中的引用 s1 指的是不同的字符串。
  • Concat2 :在此方法中,我们传递一个字符串“Geeks”并执行“s2.append(“forgeeks”)”,将字符串的实际值(在 main 中)更改为“Geeksforgeeks”。这是因为 StringBuilder 是可变的,因此会更改其值这一简单事实。
  • Concat3 : StringBuilder 类似,除了线程安全的关键区别外,在所有地方都可以与 StringBuffer 兼容。 StringBuffer 是线程安全的,而 StringBuilder 不保证线程安全,这意味着 StringBuffer 中存在同步方法,从而一次控制一个线程访问,而在 StringBuilder 中看不到它,因此是线程不安全的。

Java中字符串类型之间的转换

有时需要将不同类(如 String、StringBuffer、StringBuilder)的字符串对象相互转换。以下是一些可以做到这一点的技术。让我们涵盖所有用例如下:

  1. 从 String 到 StringBuffer 和 StringBuilder
  2. 从 StringBuffer 和 StringBuilder 到 String
  3. 从 StringBuffer 到 StringBuilder 或反之亦然

案例一:从String到StringBuffer和StringBuilder  

这是一种简单的方法,因为我们可以直接将 String 类对象传递给 StringBuffer 和 StringBuilder 类的构造函数。由于 String 类在Java中是不可变的,因此对于编辑字符串,我们可以通过将其转换为 StringBuffer 或 StringBuilder 类对象来执行相同的操作。

例子

Java

// Java program to demonstrate conversion from
// String to StringBuffer and StringBuilder
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Custom input string
        String str = "Geeks";
 
        // Converting String object to StringBuffer object
        // by
        // creating object of StringBuffer class
        StringBuffer sbr = new StringBuffer(str);
 
        // Reversing the string
        sbr.reverse();
 
        // Printing the reversed string
        System.out.println(sbr);
 
        // Converting String object to StringBuilder object
        StringBuilder sbl = new StringBuilder(str);
 
        // Adding it to string using append() method
        sbl.append("ForGeeks");
 
        // Print and display the above appended string
        System.out.println(sbl);
    }
}
输出
skeeG
GeeksForGeeks

案例2:从StringBuffer和StringBuilder到String

可以使用在 StringBuffer 和 StringBuilder 类中重写的toString()方法执行此转换。
下面是演示相同的Java程序。注意,当我们使用toString()方法时,会分配一个新的 String 对象(在 Heap 区域)并初始化为 StringBuffer 对象当前表示的字符序列,这意味着后续对 StringBuffer 对象的更改不会影响到 StringBuffer 对象的内容。字符串对象。

例子

Java

// Java Program to Demonstrate Conversion from
// String to StringBuffer and StringBuilder
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Creating objects of StringBuffer class
        StringBuffer sbr = new StringBuffer("Geeks");
        StringBuilder sbdr = new StringBuilder("Hello");
 
        // Converting StringBuffer object to String
        // using toString() method
        String str = sbr.toString();
 
        // Printing the above string
        System.out.println(
            "StringBuffer object to String : ");
        System.out.println(str);
 
        // Converting StringBuilder object to String
        String str1 = sbdr.toString();
 
        // Printing the above string
        System.out.println(
            "StringBuilder object to String : ");
        System.out.println(str1);
 
        // Changing StringBuffer object sbr
        // but String object(str) doesn't change
        sbr.append("ForGeeks");
 
        // Printing the above two strings on console
        System.out.println(sbr);
        System.out.println(str);
    }
}
输出
StringBuffer object to String : 
Geeks
StringBuilder object to String : 
Hello
GeeksForGeeks
Geeks

案例 3:从 StringBuffer 到 StringBuilder,反之亦然

这种转换很棘手。没有直接的方法来转换它。在这种情况下,我们可以使用 String 类对象。我们首先使用toString()方法将 StringBuffer/StringBuilder 对象转换为 String,然后使用构造函数从 String 转换为 StringBuilder/StringBuffer。

例子

Java

// Java program to Demonstrate conversion from
// String to StringBuffer and StringBuilder
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Creating object of StringBuffer class and
        // passing our input string to it
        StringBuffer sbr = new StringBuffer("Geeks");
 
        // Storing value StringBuffer object in String and
        // henceforth converting StringBuffer object to
        // StringBuilder class
        String str = sbr.toString();
        StringBuilder sbl = new StringBuilder(str);
 
        // Printing the StringBuilder object on console
        System.out.println(sbl);
    }
}
输出
Geeks

从上述三个用例中,我们可以得出以下结论:

  • String 的对象是不可变的,StringBuffer 和 StringBuilder 的对象是可变的。
  • StringBuffer 和 StringBuilder 相似,但对于单线程程序,StringBuilder 比 StringBuffer 更快且更受欢迎。如果需要线程安全,则使用 StringBuffer。