DEV Community

Anees Abdul
Anees Abdul

Posted on

String in Java

What is a String:

  • A string is a sequence of characters.
  • String is an immutable object, and it is in java.lang.String package.
  • It implements interfaces like Serializable, Comparable, and CharSequence interfaces.
  • CharSequence is the parent interface of String. It is also implemented by StringBuffer and StringBuilder.

String is an Immutable Object:

  • String is an immutable object, which means it is constant and cannot be changed
  • It is stored in a special memory area called the String Constant Pool (SCP)
  • The String Constant Pool (SCP) is a special memory area inside the heap.

Ways of creating a string in Java:

  1. String literal (Static Memory)
  2. Using new keyword (Heap Memory)

How to create a string literal:

String s1="Java";

When you create a string using a literal, Java first checks:

-If the same string already exists in SCP → it reuses it
Otherwise → it creates a new object

String s1 = "Java";
String s2 = "Java";

->Both s1 and s2 point to the same memory location in the String Pool.

Enter fullscreen mode Exit fullscreen mode
 public static void main(String[] args)
    {
        String s = "Test";

        // concat() method appends the string at the end
        s.concat(" Automation");

        // This will print Test because strings are immutable objects
        System.out.println(s);
    }
O/P: Test
Enter fullscreen mode Exit fullscreen mode

As we can see in the given figure, two objects are created, but the reference variable still refers to "Test" and not to "Automation". But if we explicitly assign it to the reference variable, it will refer to the "Test Automation" object.

 public static void main(String[] args)
    {
        String name = "Test";
        name = name.concat(" Automation");
        System.out.println(name);
    }
O/P: Test Automation

Enter fullscreen mode Exit fullscreen mode

How to create a String using new Keyword:

String s = new String("Welcome");

A String object is explicitly created in Heap memory, even if the same value already exists in the String Constant Pool.

🔹 Commonly Used String Methods in Java

1️⃣ length()

Returns the length of the string

String s = "Java";
System.out.println(s.length()); // 4

2️⃣ charAt(int index)

Returns the character at the specified index

System.out.println(s.charAt(1)); // a

3️⃣ equals(String s)

Compares content of two strings

String a = "Java";
String b = "Java";
System.out.println(a.equals(b)); // true

4️⃣ equalsIgnoreCase(String s)

Compares strings ignoring case

System.out.println(a.equalsIgnoreCase("java")); // true

5️⃣ compareTo(String s)

Compares strings lexicographically

System.out.println("Java".compareTo("Java")); // 0

6️⃣ toUpperCase()

Converts string to uppercase

System.out.println("java".toUpperCase());

7️⃣ toLowerCase()

Converts string to lowercase

System.out.println("JAVA".toLowerCase());

8️⃣ trim()

Removes leading and trailing spaces

String s = " Java ";
System.out.println(s.trim());

9️⃣ contains(CharSequence s)

Checks if a string contains a sequence

System.out.println("Java Programming".contains("Java"));

🔟 startsWith(String prefix)

Checks starting characters

System.out.println("Java".startsWith("Ja"));

1️⃣1️⃣ endsWith(String suffix)

Checks ending characters

System.out.println("Java".endsWith("va"));

1️⃣2️⃣ substring(int beginIndex)

Extracts substring from index

System.out.println("Java".substring(1)); // ava

1️⃣3️⃣ substring(int begin, int end)

Extracts substring between indexes

System.out.println("Java".substring(1, 3)); // av

1️⃣4️⃣ replace(char old, char new)

Replaces characters

System.out.println("Java".replace('a', 'o'));

1️⃣5️⃣ split(String regex)

Splits string into array

String[] words = "Java is easy".split(" ");

Top comments (0)