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!
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:
Java differentiates between String literals (efficiently stored in the String pool) and Strings created via the new keyword (new objects on the heap):
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.
new in JavaThese 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.
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.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
There are two primary ways to create Strings in Java:
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:
"igmGuru".System.out.println(demoString) is executed, it displays the value of demoString on the console: igmGuru.new KeywordIn 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:
new keyword to create a String object named str with the value "igmGuru".System.out.println(str) runs, it outputs the content of str to the console: igmGuru.Read Also: Java Interview Questions and Answers
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.
Java Strings support Unicode, which means you can store characters from almost any language along with emojis.
Yes, Java Strings are immutable, making them naturally thread-safe. Multiple threads can use the same String without causing any issues.
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.
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 |