String, StringBuilder, and StringBuffer
String, StringBuilder, and StringBuffer
. . .
. . .
1. String :
The String class represents character strings. All string literals in Java
programs, such as “ABC”, are implemented as instances of this class.
Strings are constant; their values cannot be changed after they are Top highlight
There are two ways to create string objects: Using string literal and
other is using a new keyword.
1
2 public class StringExample {
3
4 public static void main(String[] args) {
5 String name1 = "Vikram";
6 //here string literal used to create string object
7
8 String name2 = new String("Gupta");
9 //here new keyword is used to create string object
10 }
11 }
1
2 public class Main {
3
4 public static void main(String[] args) {
5
6 String name1 = "Tom";
7 String name2 = "Tom";//name1.intern();
8
9 String name3 = new String("Bob");
10 String name4 = "Bob";
11
12 System.out.println(name1 == name2);
13 System.out.println(name3 == name4);
14
15 }
16 }
OUTPUT :
true
false
Below are String class methods. They can be used as an individual method
or in combination.
. . .
2.StringBuilder :
StringBuilder represents a mutable sequence of characters.
The instance of this class does not guarantee Synchronization and hence
should not be used in multi-threaded environments.
Below are StringBuilder class methods and descriptions. They can be used
as an individual method or in combination.
1
2 public class Main {
3
4 public static void main(String[] args) {
5
6 StringBuilder stringBuilder = new StringBuilder("Vikram");
7 System.out.println(stringBuilder);
8 stringBuilder.append(" Gupta");
9 System.out.println(stringBuilder);
10 stringBuilder.insert(7," Binod");
11 System.out.println(stringBuilder);
12 }
13 }
OUTPUT:
Vikram
Vikram Gupta
Vikram Binod Gupta
Note: insert method can be used for appending the string at the end of
the specified string using the length() function.
stringBuilder.insert("Vikram".length()," Gupta");
//this results "Vikram Gupta"
. . .
3.StringBuffer :
StringBuffer class represents a thread-safe, mutable sequence of
characters.
The append method always adds these characters at the end of the
buffer and the insert method adds the characters at a specified point.
Below are StringBuffer class methods and descriptions. They can be used as
an individual method or in combination. These methods are synchronized
and can be used for a multi-threaded environment.
String bu er example
OUTPUT:
Vikram
Vikram Gupta
Vikram Binod Gupta
. . .
1
2 import java.util.Calendar;
3
4 public class Main {
5
6 public static void main(String[] args) {
7
8 System.gc();
9 StringBuilder stringBuilder = new StringBuilder("Vikram");
10 long startTime1 = Calendar.getInstance().getTimeInMillis();
11 for (long i = 0; i < 10000000; i++) {
12 stringBuilder.append(i);
13 }
14 long endTime1 = Calendar.getInstance().getTimeInMillis();
15 System.out.println("Time taken for 10000000 appends for StringBuilder:" + (endTime1 - st
16
17 System.gc();
18 StringBuffer stringBuffer = new StringBuffer("Vikram");
19 long startTime2 = Calendar.getInstance().getTimeInMillis();
20 for (long i = 0; i < 10000000; i++) {
21 stringBuffer.append(i);
22 }
23 long endTime2 = Calendar.getInstance().getTimeInMillis();
24 System.out.println("Time taken for 10000000 appends for StringBuffer:" + (endTime2 - sta
25
26 }
27 }
OUTPUT:
The below out is an average of 20 runs and we can see the difference.
. . .
That’s it for this article. I hope you have understood the concept of String,
StringBuilder, and StringBuffer. If you find this article helpful, you can
follow me.
medium.com
WRITTEN BY
Vikram Gupta Follow
How we wrote the Why I Am Relearning This is What it Takes to Replacing If-Else With
Fastest JavaScript UI Angular Be a Great Front-End Commands and Handlers
Framework, Again! Bharath Ravi in Level Up Developer These Days Nicklas Millard in Level Up
Ryan Carniato in Level Up Coding Daan in Level Up Coding Coding
Coding
Write Clean Code With Use VSCode Like a Senior 10 Useful Golang 4 Things You Have to
These 5 Simple Tips Developer Modules Developers Unlearn to Become A
Daan in Level Up Coding bit sh in Level Up Coding Should Know. Better Programmer
Bryan Dijkhuizen, BA. in Level Li-Hsuan Lung in Level Up
Up Coding Coding