Creating a string in Java looks deceptively simple. In most programs, you will write something like String name = "Asha"; and move on. But Java actually provides more than one way to create a string, and the choice can affect how objects are stored and reused.
Understanding string creation is therefore not just about memorizing syntax. It helps you understand references, objects, the String Pool, memory usage, and why two strings that look identical can behave differently when compared incorrectly.
Creating a String Using a String Literal
The most common and recommended way to create a string is by using a string literal. A string literal is text enclosed in double quotation marks.
String language = "Java"; String message = "Welcome to Java"; String country = "India";
Java automatically creates or reuses an appropriate string object for the literal. You do not need to explicitly use the new keyword in this common form.
For everyday programming, this is usually the cleanest approach because it is concise, readable, and allows Java to take advantage of the String Pool.
When you simply need a fixed text value, prefer a string literal such as "Java" instead of unnecessarily creating a separate object with new.
Creating a String Using new
Java also allows you to explicitly create a String object using the new keyword.
String language = new String("Java");
This syntax tells Java to create a new String object. Even though the resulting text is "Java", this approach is different from simply writing a string literal.
In normal application code, explicitly using new String() for a literal is rarely necessary. It can create an additional String object when the same text could have been represented using the pooled literal.
String Literal vs new String()
Consider the following example:
String first = "Java";
String second = "Java";
String third = new String("Java");
The variables first and second use the same literal value. Java can reuse the existing pooled string instead of creating another identical pooled object.
The variable third, however, explicitly requests a new String object. The text is still "Java", but the object creation mechanism is different.
| Creation Style | Example | Typical Behaviour |
|---|---|---|
| String literal | "Java" | Uses the String Pool |
| new String() | new String("Java") | Explicitly creates a String object |
Understanding the String Pool
The String Pool is a special area associated with the Java runtime's handling of string literals. Its purpose is to avoid unnecessarily creating multiple identical string objects.
Imagine a large application where thousands of classes repeatedly use the text "SUCCESS". Creating a completely separate object for every occurrence would be wasteful if the same immutable text could safely be shared.
String a = "SUCCESS"; String b = "SUCCESS"; String c = "SUCCESS";
Because strings are immutable, Java can safely allow these references to share the same pooled string value. If one reference could modify the shared object, this optimisation would be dangerous. String immutability is therefore closely connected to the usefulness of the String Pool.
The String Pool is possible because String objects are immutable. Java can safely reuse an existing pooled string because its character content cannot be changed after the object is created.
What Happens with Duplicate Literals?
Suppose you write:
String city1 = "Delhi"; String city2 = "Delhi";
Java does not need to create two separate pooled string objects containing the same text. The literal "Delhi" can be stored once in the String Pool and both variables can refer to that pooled object.
This is one reason string literals are generally preferred over unnecessary explicit object creation.
String Creation with a Character Array
A string can also be created from a character array. This is useful when your program already has individual characters stored in an array.
char[] letters = {'J', 'a', 'v', 'a'};
String language = new String(letters);
System.out.println(language);
The output is:
Java
Here, the character array contains the individual characters, and the String constructor creates a string from them.
Creating a String from a Portion of a Character Array
Java also provides a constructor that allows you to create a string from part of a character array.
char[] letters = {'J', 'a', 'v', 'a', ' ', '8'};
String language = new String(letters, 0, 4);
System.out.println(language);
The output is:
Java
The starting position is 0, and the length is 4. Therefore, Java takes four characters beginning at index zero.
Creating a String from Another String
A String can also be passed to the String constructor.
String original = "Java"; String copy = new String(original); System.out.println(copy);
The output is:
Java
Although this syntax is valid, using new String(original) simply to duplicate a string is usually unnecessary because String objects are immutable.
Creating a String from a Byte Array
Strings can also be created from byte arrays. This is particularly important when working with files, networks, APIs, and encoded data.
byte[] data = {74, 97, 118, 97};
String language = new String(data);
System.out.println(language);
The output is:
Java
In real applications, character encoding should be considered explicitly when converting bytes to text. The meaning of a byte sequence depends on the encoding used.
When converting bytes to strings in production applications, avoid assuming an encoding unless it is guaranteed by the data source. Explicit character sets such as UTF-8 make the conversion predictable.
String Creation Does Not Mean String Modification
A subtle but important point is that creating a string and modifying a string are different ideas. Once a String object exists, its character content cannot be changed. Operations that appear to modify a string actually produce another string.
String language = "Java"; language = language.toUpperCase(); System.out.println(language);
The output is:
JAVA
The original String object containing "Java" was not modified. The operation produced another string containing "JAVA", and the variable was then assigned to that result.
This behaviour becomes much easier to understand once String immutability is studied in detail.
Common Beginner Mistake: Using new Everywhere
A beginner may assume that because String is a class, every String should be created using new.
String name = new String("Rahul");
There is nothing syntactically wrong with this statement, but it is usually unnecessary when a literal is sufficient.
A cleaner approach is:
String name = "Rahul";
The second form communicates intent more clearly and allows Java to use the String Pool for the literal.
Common Beginner Mistake: Confusing Reference and Content
Two String variables can contain the same characters without necessarily being references to the same object.
String a = "Java";
String b = new String("Java");
Both represent the text Java, but they are created differently. This distinction becomes important when using == and equals().
When the goal is to determine whether two strings contain the same text, use content comparison with equals(), not reference comparison with ==.
Best Practice
- Use string literals for ordinary fixed text.
- Avoid new String("text") unless you have a specific reason to create a distinct String object.
- Use character-array constructors when your source data is already a character array.
- Specify the appropriate character encoding when converting external byte data into text.
- Remember that creating a String object and changing a String variable are separate concepts.
- Use equals() when comparing string content.
Interview Insight
A classic interview question is: “What is the difference between String a = "Java" and String b = new String("Java")?” The key idea is that the literal form uses Java's String Pool mechanism, while the new expression explicitly creates a new String object. The two forms may represent identical text, but their object creation and reference behaviour differ.
String Creation at a Glance
| Approach | Example | When to Use |
|---|---|---|
| String literal | "Java" | Normal text values |
| new String() | new String("Java") | Rare cases requiring an explicit String object |
| Character array | new String(chars) | When characters already exist in an array |
| Byte array | new String(bytes) | Converting encoded byte data into text |
| Another String | new String(original) | Usually unnecessary because String is immutable |
Final Takeaway
Java provides several ways to create strings, but the string literal is the natural choice for most everyday programming. The String Pool allows identical literals to be reused, while new String() explicitly creates a String object and is rarely needed for ordinary text. You can also construct strings from character arrays, byte arrays, or other strings when your application requires it. Once you understand how these creation techniques differ, the next concept becomes much more intuitive: why Java deliberately prevents an existing String object's content from being changed—String Immutability.
