Strings in Java | String Class

Beginners Code Computer Skills Java Mobile Development Web Development

Generally, String is a sequence of characters in languages like C and C++. But in Java, a String is an object. It is a predefined class present in java.lang package which is used to create a string object

There are a lot of built-in methods and constructors in String class that allow a lot of operations like concatenating strings, trimming a string, extracting substring, finding the length of a string and many more.

How to create a String object?

There are 2 ways to create a String object:

  • By String literal
  • By new keyword

Using String literal:

  • String literal is created by a double quote.
    • For Example: String s = “Hello”;
  • If the string is created by the help of string literal, it allocates memory from string constant pool.
    • String constant pool is a temporary memory which never stores any duplicate strings.
  • Each time we create a string literal, the JVM checks for the presence of that string in the constant pool first.
    • If that string already exists in the pool, a reference to the pooled instance returns.
    • If that string does not exist in the pool, a new String object is created and it is placed in the string constant pool for further references.
String string1 = "Computer Programming";
String string2 = "Computer Programming";

 

string reference

 

In the above example,

  • for string1:
    • it creates a new string object as JVM does not find any string “Computer Program” in the pool.
  • for string2:
    • as already string “Computer Program” is present in string constant pool so it will not create a new object.
    • It will only return the reference to the same instance.

Why java uses the concept of string literal?

In case of creation of string through String literal, “no new objects are created if it exists already in string constant pool”. Hence it makes Java more memory efficient.

Using the new keyword:

  • In this case, JVM will create a new String object in normal (non-pool) heap memory and then the literal “Computer Program” will be placed in the string constant pool.
  • Now, the variable str will refer to the object in Heap (non-pool).
  • For example,  String str = new String("Computer Programming");

Strings are Immutable in Java:

In Java, strings are immutable (un-modifiable) objects.

package test;

import java.lang.String;

public class StringDemo {

  public static void main(String[] args) {
    
    String str = "Java";
    str.concat("Language");
    System.out.println("The final string is: " + str);
      
 }	
}

Output:

No concat string

You can see that in the above program, the existing value of str did not change.

package test;

public class StringTest {
  
  public static void main(String[] args) {
    String str = "Java";
    str = str.concat("Language");
    System.out.println("The final string is: " + str);
  }

}

Output:

concat string

In the above example, the output is “JavaLanguage” because here we are assigning the value to the existing value not concatenating.

Why is String object immutable in Java?

  • Java uses the concept of string literal.
  • Suppose there are 4 reference variables and all of them refer to the same object.
    • If one reference variable’s value changes the value of the object, it will affect the values of all the reference variables.
    • That is why strong objects are immutable in java.

Objective: Program on basic operations on String using predefined methods of String class:

package test;

public class StringOperations {

  public static void main(String[] args) {
    
    String str = "Welcome to the World of Java !";				
    System.out.println("The original string is " + str);
    
    String str1 = str.concat("Java Programming is easy");
    System.out.println("\n The final string is " + str1);
    
    System.out.println("\n The length of the strings are: str:" + str.length()+" str1:" + str1.length());
    
    String str2 = str.substring(5, 24);
    System.out.println("\nThe substring is: " + str2);
    
    String str3 = str.toUpperCase();
    System.out.println("\nThe string in uppercase: " + str3);
    
    System.out.println("\nCharacter at position 15: " + str.charAt(15));
    
    // Returns the index within this string of the first occurrence
    System.out.println("\nIndex of letter d in str: " + str.indexOf('d'));
    System.out.println("\nIndex of letter W in str: " + str.indexOf('W'));
    
    System.out.println("\nIndex of string easy in str1: " + str1.indexOf("easy"));    
  }
}

Output:

String operations

 

This article contains a brief description and an introduction to String class present in Java. You can learn more about it from the official doc.

The next section in this series will contain the basic terminologies and implementation of OOPS concepts in Java like inheritance, polymorphism, abstract classes and interface.