Handling Strings in Java

String is a sequence of characters that can be used to store and manipulate text. There are several ways to handle strings in Java. Which can be used to store and manipulate text in a program. In this blog post, we will cover the different topics in handling Strings in Java along with the frequently asked questions.

Creation of String in Java

There are several ways to create a string in Java. The most common way is to use double quotes to create a string literal, like this:

String str = "Hello, World!";

We can also use the String class’ constructor to create a string:

char[] helloArray = { 'h', 'e', 'l', 'l', 'o' };
String helloString = new String(helloArray);



String concatination in Java

We can concatenate two strings using the + operator, like this:

String str1 = "Hello";
String str2 = "World";
String str3 = str1 + ", " + str2 + "!";


Hello, World!

String conversion in Java

We can also convert other data types to strings using the String.valueOf() method. For example:

int num = 123;
String str = String.valueOf(num);

Changing case

Java provides two methods to change the case of a string. These methods include toLowerCase() and toUpperCase(). Following is an example of how to use these methods:

String str = "Hello, World!";
str = str.toLowerCase(); 
System.out.println("Lower Case: "+str);
str = str.toUpperCase();
System.out.println("Upper Case: "+str);


Lower Case: hello, world!
Upper Case: HELLO, WORLD!

Character extraction

We can extract a single character from a string using the charAt() method. This method takes an index as an argument and returns the character at that index. For example:

String str = "Hello, World!";
char first = str.charAt(0); 
char last = str.charAt(str.length() - 1); 

System.out.println("First Character: "+first);
System.out.println("Last Character: "+last);


First Character: H
Last Character: !

String comparison

Java provides several ways to compare strings. The most common way is to use the equals() method, which compares the contents of two strings. For example:

String str1 = "Hello, World!";
String str2 = "Hello, World!";
if (str1.equals(str2)) {
  System.out.println("The strings are equal.");
} else {
  System.out.println("The strings are not equal.");


The strings are equal.


Java provides different methods to search for a particular character or substring within a string. The indexOf() method returns the index of the first occurrence of a character or substring within a string.

For example:

String str = "Hello, World!";
int index = str.indexOf('o'); 



We can also use the lastIndexOf() method to find the index of the last occurrence of a character or substring within a string. For example:

String str = "Hello, World!";
int index = str.lastIndexOf('o');




As we all know array index starts from 0 (Zero), the string character count is also starts from 0. This is the reason that, the above two example code prints 4 and 8 insted of 5 and 9.

Modify String in Java

In Java, strings are immutable, which means that once a string is created, it cannot be modified. However, we can create a new string that is a modified version of an existing string. For example, we can use the replace() method to replace a particular character or substring with a new character or substring.

Let’s see an example below:

String str = "Hello, World!";
str = str.replace('o', 'O');  // replaces lowercase o with upper case O


"HellO, WOrld!"

We can also use the substring() method to extract a portion of a string. This method takes two arguments: the start index and the end index of the substring. For example:

String str = "Hello, World!";
String sub = str.substring(6, 11); 



StringBuffer and StringBuilder

In addition to the String class, Java provides two other classes for handling strings. These are:

  • StringBuffer and
  • StringBuilder

These classes are similar to String but they are mutable, which means that we can modify their contents.

StringBuffer is thread-safe, which basically means that it can be used in a multithreaded environment without any issues. However, it is slower than StringBuilder because StringBuffer synchronizes every method call to ensure thread safety.

StringBuilder is not thread-safe, but it is faster than StringBuffer because it does not synchronize every method call. It is generally recommended to use StringBuilder unless thread safety is required.

Following is an example of how to use StringBuilder:

StringBuilder sb = new StringBuilder("Hello");
sb.append(", World!");

The output of the above program is:

Hello, World!


Java provides several ways to handle strings, including creation, concatenation, conversion, case changing, character extraction, comparison, searching, and modification. It also provides the StringBuffer and StringBuilder classes for handling strings in a multithreaded environment or for optimizing performance.


What is a string in Java?

A string in Java is a sequence of characters. Strings are represented internally as an array of Unicode characters.

How to create a string in Java?

There are two ways to create a string in Java:

Using string literals: String literals are enclosed in double quotes. For example, String s = "Coder Sathi!";.

Using the new keyword: The new keyword is used to create a new instance of a class. For example, String s = new String("Coder Sathi");.

Why is the String class immutable?

The String class is immutable, meaning that once a string object is created, its value cannot be changed. This makes strings thread-safe, this means that they can be safely used by multiple threads without any synchronization.

What are some common string handling mistakes to avoid?

The common string handling mistakes to avoid are:

Modifying a string literal. String literals are immutable, so you cannot modify them.

Using the == operator to compare strings instead of to use the equals() method.

Using mutable string classes, such as StringBuffer and StringBuilder, unnecessarily.

Notify of
Inline Feedbacks
View all comments