Skip to content

Module 2: Strings

Rishabh Malviya edited this page Feb 7, 2023 · 1 revision

Strings

String in Java

  • A string is a sequence of characters.
  • The String class is immutable, which means that once a String object is created, it cannot be changed.
  • Strings are stored in the String Pool in the heap memory.
  • There are different methods are available in the String class to perform operations on strings.

To create a string, use the following syntax:

    class Main {
    public static void main(String[] args) {
        String str = "Hello World";
        String str1 = new String("Hello World");
        String str2 = new String(new char[]{'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd'});
        String str3 = new String(new byte[]{72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100});
        System.out.println(str, str1, str2, str3);
    }
}

String Buffer

  • The StringBuffer class in Java is used to create mutable (modifiable) string.
  • The StringBuffer class in Java is not-synchronized means two threads can't call the methods of StringBuffer simultaneously.
  • It is less efficient than StringBuilder class.
class Main{
    public static void main(String[] args){
                StringBuffer str = new StringBuffer("Hello");
                str.append(" World");
                System.out.println(str);
    }
}

String Builder

  • The StringBuilder class in Java is used to create mutable (modifiable) string.
  • The StringBuilder class in Java is same as StringBuffer class except that it is non-synchronized.
  • It is more efficient than StringBuffer class.
  • Both StringBuffer and StringBuilder has different methods to perform operations on strings.
class Main{
    public static void main(String[] args){
                StringBuilder str = new StringBuilder("Hello");
                str.append(" World");
                System.out.println(str);
    }
}

String Manipulation in Java

  • The String class in Java is immutable, which means it cannot be changed.
  • String manipulation is changing the value of a string.
  • There are lots of methods available in the String class to perform operations on strings.
  • The result of the operation is always a new string.

Strings Methods

String s = new String("Hello World");
s.length(); // returns the length of the string
s.charAt(0); // returns the character at the specified index
s.indexOf('o'); // returns the index of the specified character
s.substring(0, 5); // returns the substring from the specified index
s.toUpperCase(); // returns the string in uppercase
s.toLowerCase(); // returns the string in lowercase
s.trim(); // removes the leading and trailing spaces
s.replace('o', 'a'); // replaces the specified character with another character
s.startsWith("Hello"); // checks if the string starts with the specified string
s.endsWith("World"); // checks if the string ends with the specified string
s.equals("Hello World"); // checks if the string is equal to the specified string

Similarly more methods are available in the String class to perform operations on strings.

Click here to view all the methods available in the String class.

String and Memory

  • Java stores strings in the String Pool in the heap memory. The String Pool is a special memory area in the heap memory.
  • Whenever we perform any operation on string a new string is created in the String Pool. And the old one deletes everytime.
  • Operations on strings are more time taking as compared to other data types. to overcome this java introduced StringBuffer and StringBuilder classes.
  • The Operations on StringBuffer and StringBuilder are less time taking as compared to String class.
  • There are two ways to create a string in Java.
    • String Literal
      • When we are creating a new string using string literal, if the string is already present in the String Pool then the string is not created again.
    • Using new keyword
      • When we are creating a new string using new keyword, a new string is always created in the String Pool.

Clone this wiki locally