String Manipulation

 

String Manipulation

  1. String 
  2. StringBuffer 
  3. StringBuilder

Memory in Java is divided into three parts such as heap, stack, and String Pool. The string is so important in Java that it has a dedicated memory location.

1. String 

  • Def : String is an object of String class that represents a sequence of characters.
  • In C/C++ programming languages, it represents an array of characters, where the last character will be the null character (\0) that represents the end of the string.
  • Strings are objects in Java.
  • String object is immutable in Java.
  • String is not a primitive data type. It is a reference data type.
  • string is fixed-length, immutable, and cannot be modified.

Cration String object in Java
  1. string literal.
  2. new keyword.
  3. converting character arrays into strings

Examples 
String literal: String str = "Hello World";
Using the new keyword: String str = new String("Hello World");
Conversion from character array: char[] charArray = {'H', 'e', 'l', 'l', 'o'}; 
String str = new String(charArray);

1. String literal in Java

- String literal in Java is created by using double quotes.
- The string literal is always created in the string constant pool. In Java, String constant pool is a special area that is used for storing string objects.
- String constal pool area is store in the heap memory .
- A string object is created for every string literal.


For example:

String s1 = "Hello";
String s2 = "Hello";




2. Creating String Object by new Keyword

- Creating an object to string class is by using new operator.
- For every string literal, one copy will be created in the String Constant Pool area.

Ex :
String s = new String("Hello");




  • JVM will create the second object as a copy for literal “Hello” in string constant pool for future purposes.
  • Whenever we will create a string object using the new operator, compulsory a new object will be created in the heap area and another one copy will be created in the string constant pool for further future purposes.
  • For every string literal, JVM will create a string object in the string constant pool.

( Note : The object created in the SCP area is not eligible for garbage collection because implicitly, the reference variable will be maintained by JVM itself.)




3. By converting Character Arrays into String

The third way to create strings is by converting the character arrays into string. 

Ex :
char arr[ ] = {'j','a','v','a'};

Now create a string object by passing array name to string constructor like this:

String s = new String(arr);


Why String objects are immutable in Java?
String objects are immutable in Java because Java uses the concept of string constant pool.


2.StringBuffer Class in Java

  • - Java StringBuffer class is used to create mutable (modifiable) String objects. 
  • StringBuffer in Java is a peer class of String that provides much of the functionality of strings. It provides more flexibility than String.
  • To overcome string, Java introduced another class called StringBuffer.
  • Java StringBuffer represents a growable nature and mutable(change).
  • StringBuffer class method is synchronized. Therefore, it is a thread-safe class but it is slower than string.


Syntax 

StringBuffer sb = new StringBuffer();


3.StringBuffer Methods in Java 

  • append()
  • capacity()
  • charAt()
  • delete()
  • ensureCapacity()
  • getChars()
  • indexOf()
  • insert()
  • length()
  • reverse() and many more.


3. Java StringBuilder Class

  • Java StringBuilder class represents a mutable sequence of characters (string).
  • StringBuffer is thread-safe and StringBuilder is not thread-safe. 
  • StringBuilder is suitable to use when no thread-safety is needed.

Syntax 

StringBuilder sb = new StringBuilder();



When to Use String, StringBuffer, or StringBuilder in Java?

1. String is suitable to use when you do not want to modify the content in the string object. String is an immutable class. Once created, its content cannot be changed.

2. StringBuffer can be used when we want to perform any changes in the content and need synchronization. It can be used when you are working in multithreading environment.

3. StringBuilder can be used when you want to create a string within a single thread. It will improve the execution time and performance.


Speed:

a) String is slower than StringBuffer and StringBuilder.

b) StringBuffer is slower than StringBuilder, but it is faster than String.

c) StringBuilder is faster than string and StringBuffer.







Comments

Popular Posts