CodeToConcept
About

java

java Tutorials

Introduction
Java Variables
Java Data Types
Java Type Casting
Java Operators
Java Input/Output
Java Class & Object
java Method
Java Constructor
Java this keyword
Java super keyword
Java Method Overloading and Overriding
Java Interface
Java Abstract Class
Java Memory Management
Design Patterns
JDBC (Database Connectivity)
Java IDEs

java Tutorials

Introduction
Java Variables
Java Data Types
Java Type Casting
Java Operators
Java Input/Output
Java Class & Object
java Method
Java Constructor
Java this keyword
Java super keyword
Java Method Overloading and Overriding
Java Interface
Java Abstract Class
Java Memory Management
Design Patterns
JDBC (Database Connectivity)
Java IDEs

Sample Question

Think it through, then check the tutorial.

CodeToConcept

Quickly learn through simple, structured, and practical coding tutorials. Build your confidence from step zero to real-world experience.

© 2026 CodeToConcept. All rights reserved.

Quick Links

AboutTutorials

Tutorials

JavaHTMLCSS

Support

Privacy PolicyTerms of ServiceContact
GitHubTwitterYouTube

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  b

2. 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

false

The two references refer to different String objects.

However:

System.out.println(a.equals(b));

Output

true

because 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

Java

4. 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

ABC

The 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

100

Other 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            d

The 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

true

Do not use == when your goal is content comparison:

System.out.println(a == b);

Output

false

Summary

Creation MethodExampleString Pool Involvement
String literal"Java"Uses the String pool
new String()new String("Java")Creates a new String object
Character arraynew String(charArray)Creates a String from characters
Byte arraynew 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....🙂

← Immutable vs Mutable StringsString Pool →
← Immutable vs Mutable StringsString Pool →