Strings in Java

What is Strings in Java?

April 6th, 2026
2086
6:00 Minutes

Strings have become an important part of the Java programming language with its features of storing names to handling user input. They form the backbone of text manipulation in any application. Strings in Java are simply objects that represent a sequence of characters. I have created this blog to help you understand what Java Strings are, their creation methods, immutability, and practical examples. Let's dive in!

What Are Strings in Java?

A Java String is an object of the A java.lang.String class that represents a sequence of characters. All string literals in Java become instances of this class, and String objects are immutable. That means their value cannot be changed once created.

A String is a sequence of characters, and the String class serves as the blueprint for creating and managing String objects. Each String represents a sequence of characters. Methods of the String class empower developers to perform the following operations:

  • Searching and comparing strings.
  • Evaluating individual characters in the string.
  • Copying strings with characters converted between uppercase and lowercase.

Master Java Programming with Java Training

Boost your coding skills and gain hands-on knowledge in Java.

Explore Now

Types of Strings in Java

Java differentiates between String literals (efficiently stored in the String pool) and Strings created via the new keyword (new objects on the heap):

1. String Literals in Java

These are created using double quotes (e.g., "Hello") and are stored in the String constant pool for memory efficiency. They are not true primitives but are treated as lightweight references. Unlike primitives (e.g., int, char), they are objects with methods and properties.

2. String Objects Created with new in Java

These are explicitly created with the new keyword, allocating a new object on the heap—even if an identical string exists in the pool. This can create separate references for the same content.

How to Create a String Object in Java: Explained With Examples

Let's understand how to create String objects in Java through the following constructors.

  • String() - Creates an empty string.
  • String(char[] arr) - Builds a new string from the given character array.
  • String(char[] arr, int offset, int count) - Creates a new string from a subsequence of the provided array (starting at offset, taking count characters).
  • String(String original) - Creates a copy of another String object.

Example of Using Java String Constructors

Here is an example demonstrating Java String constructors:

class String_Creation_Demo {

public static void main(String[] args) {

String ob1 = new String(); // Creates an empty string

// new keyword creates an object of class String

System.out.println("Empty String: '" + ob1 + "'");

char arr[] = {'j', 'k', 'a', 'q', 'e'};

String ob2 = new String(arr); // String from an array

System.out.println("Contents of Array String: " + ob2);

String ob3 = new String(arr, 1, 2); // String from subsequence of an array (offset 1, count 2)

System.out.println("Contents of Subsequence of Array String: " + ob3);

String ob4 = new String(ob3); // String from another string object

System.out.println("Contents of Copied String ob4: " + ob4);

}

}

Output

This is what the output would look like:

Empty String: ''

Contents of Array String: jkaqe

Contents of Subsequence of Array String: ka

Contents of Copied String ob4: ka

Explanation

Here is a brief explanation for the code above:

  • String ob1 = new String(): Creating an empty string - This constructor creates an empty String object (containing no characters). It prints as Empty String: ''.
  • String ob2 = new String(arr): Creating a string from a character array - It converts the provided character array {'j','k','a','q','e'} into the String jkaqe.
  • String ob3 = new String(arr, 1, 2): Creating a String from a Subsequence - This constructor builds a string from a portion of the character array, starting at index 1 and taking 2 characters ('k' and 'a'), resulting in ka.

Read Also: Java Tutorial For Beginners | Learn Java Programming for Free

Different Ways to Create Strings in Java

There are two primary ways to create Strings in Java:

1. Through String Literals

In programming, a literal is a value written directly in the code. In Java, string literals are enclosed in double quotes. You can place any text or characters between them. Literals are automatically interned in the String pool.

Syntax:

Take a look at the syntax:

String <variableName> = "<sequence_of_characters>";

Example

Here is an example for the above syntax:

public class Main {

public static void main(String args[]) {

// A string literal

String demoString = "igmGuru";

System.out.println(demoString);

}

}

Output

This is what the output would look like:

igmGuru

Explanation

Here is an explanation for the above code:

  • The code creates a String variable named demoString and assigns it the literal "igmGuru".
  • When System.out.println(demoString) is executed, it displays the value of demoString on the console: igmGuru.

2. Through new Keyword

In Java, you can use the new keyword to create a String object explicitly. When you write new String("..."), it always creates a brand new String instance on the heap, separate from the string constant pool—even if an identical value already exists in the pool. Unlike literals, which may reuse existing instances, new ensures a fresh object every time.

Syntax

Take a look at the syntax:

String stringName = new String("string_value");

Example

Here is an example:

public class Main {

public static void main(String[] args) {

String str = new String("igmGuru");

System.out.println(str);

}

}

Output

This is what the output would look like:

igmGuru

Explanation

Here is an explanation for the above code:

  • The code uses the new keyword to create a String object named str with the value "igmGuru".
  • When System.out.println(str) runs, it outputs the content of str to the console: igmGuru.

Read Also: Java Interview Questions and Answers

Conclusion

In conclusion, Java Strings may seem simple, but they hold immense power. They are the foundation of most applications, from storing basic text to handling complex data. Understanding how Strings in Java are created, made immutable, and managed in memory is essential for writing efficient and reliable Java programs.

FAQs

Q1. Can Strings contain Unicode characters?

Java Strings support Unicode, which means you can store characters from almost any language along with emojis.

Q2. Are Strings thread-safe in Java?

Yes, Java Strings are immutable, making them naturally thread-safe. Multiple threads can use the same String without causing any issues.

Q3. Why are Strings so important in Java?

Strings are one of the most commonly used data types in Java. They are crucial for handling text, user input, configuration, file names, and communication between programs.

Q4. How is a String different from a StringBuffer?

String cannot be changed after it is created. StringBuffer can be changed without creating a new object. StringBuffer is better when you need to modify text many times.

Course Schedule

Course Name Batch Type Details
Java Training Every Weekday View Details
Java Training Every Weekend View Details
About the Author
Author Nehal Sharma
About the Author

Nehal Sharma is a skilled Data Analyst with expertise in Java, mobile development, and data analytics. She transforms complex data into actionable insights and has experience in business intelligence, data science, and Salesforce. She also simplifies technical concepts into clear, engaging content for learners and professionals.

Drop Us a Query
Fields marked * are mandatory
×

Your Shopping Cart


Your shopping cart is empty.