Ways to Create String in Java
We can obtain a String from a literal, characters, bytes, or another value that needs a text form. These choices are not interchangeable: bytes need decoding, while new String(...) creates a separate object. This page follows those differences with small examples.
Understanding these approaches is important because they interact differently with the String pool.
1. Using a String Literal
The simplest and most common way is:
String name = "John";Here, "John" is a String literal.
Java checks the String pool when a literal is used.
If an equal String already exists in the pool, Java can reuse it.
Example
String a = "Java";
String b = "Java";Both variables can refer to the same pooled String object.
Conceptually:
String Pool
"Java"
↑ ↑
a b2. Using the new Keyword
We can explicitly create a String object using new:
String name = new String("John");This creates a new String object, even if an equal String already exists in the pool.
For example:
String a = "Java";
String b = new String("Java");
System.out.println(a == b);Output
falseThe two references refer to different String objects.
However:
System.out.println(a.equals(b));Output
truebecause their contents are equal.
3. Creating String from a Character Array
We can create a String from a char array.
char[] letters = {'J', 'a', 'v', 'a'};
String text = new String(letters);
System.out.println(text);Output
Java4. Creating String from a Byte Array
A String can also be created from a byte array.
byte[] data = {65, 66, 67};
String text = new String(data);
System.out.println(text);Output
ABCThe actual characters depend on the character encoding used. For predictable results with text data, specify the intended charset when appropriate.
For example:
String text = new String(data, java.nio.charset.StandardCharsets.UTF_8);5. Using String.valueOf()
The String.valueOf() method can convert primitive values and objects into their String representation.
int number = 100;
String text = String.valueOf(number);
System.out.println(text);Output
100Other examples:
String a = String.valueOf(10);
String b = String.valueOf(true);
String c = String.valueOf(10.5);String Literal vs new String()
Consider:
String a = "Java";
String b = "Java";
String c = new String("Java");
String d = new String("Java");Conceptually:
String Pool
"Java"
/ \
a b
Heap
"Java" "Java"
↑ ↑
c dThe literal references can share the pooled object.
The new expressions create distinct String objects.
Which Way Should You Normally Use?
For normal String values, use String literals:
String name = "John";There is usually no need to write:
String name = new String("John");The literal form is simpler and allows Java to use the String pool.
Important Point About new String()
When you write:
new String("Java")the literal "Java" is itself a String literal and may be in the String pool, while new String(...) creates another String object.
Therefore:
String a = "Java";
String b = new String("Java");can result in two distinct objects.
Comparing Strings Created in Different Ways
Use equals() to compare String contents:
String a = "Java";
String b = new String("Java");
System.out.println(a.equals(b));Output
trueDo not use == when your goal is content comparison:
System.out.println(a == b);Output
falseSummary
| Creation Method | Example | String Pool Involvement |
|---|---|---|
| String literal | "Java" | Uses the String pool |
| new String() | new String("Java") | Creates a new String object |
| Character array | new String(charArray) | Creates a String from characters |
| Byte array | new String(byteArray, charset) | Decodes bytes with the chosen charset |
| String.valueOf() | String.valueOf(100) | Converts a value to text |
Key Takeaway
The most common way to create a String is by using a String literal. Java can reuse equal literals from the String pool. Using new String() explicitly creates a separate String object.
See you in next page....🙂