← 输入框|www.baeldung.com|刷新
Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

1. Introduction

Java String is one of the most important classes and we’ve already covered a lot of its aspects in our String-related series of tutorials.

In this tutorial, we’ll focus on String initialization in Java.

2. Creation

First of all, we should remember how Strings are created in Java.

We can use the new keyword or the literal syntax:

String usingNew = new String("baeldung");
String usingLiteral = "baeldung";

And, it’s also important that we understand how Strings are managed in a specialized pool.

3. String Declaration Only

First, let’s just declare a String, without assigning a value explicitly.

We can either do this locally or as a member variable:

public class StringInitialization {

    String fieldString;

    void printDeclaredOnlyString() {
        String localVarString;
        
        // System.out.println(localVarString); -> compilation error
        System.out.println(fieldString);
    }
}

As we can see, if we try to use localVarString before giving it a value, we’ll get a compilation error. On the other hand, the console will show “null” for fieldString‘s value.

See, member variables are initialized with a default value when the class is constructed, null in String‘s case. But, we have to initialize local variables ourselves.

If we give localVarString a value of null, we’ll see that the two are, indeed, now equal:

String localVarString = null;
assertEquals(fieldString, localVarString);

4. String Initialization Using Literals

Let’s now create two Strings using the same literal:

String literalOne = "Baeldung";
String literalTwo = "Baeldung";

We’ll confirm that only one object is created by comparing the references:

assertTrue(literalOne == literalTwo);

The reason for this harks back to the fact that Strings are stored in a pool. literalOne adds the String “baeldung” to the pool, and literalTwo reuses it.

5. String Initialization Using new

We’ll see some different behavior, though, if we use the new keyword.

String newStringOne = new String("Baeldung");
String newStringTwo = new String("Baeldung");

Although the value of both Strings will be the same as earlier, we’ll have to different objects this time:

assertFalse(newStringOne == newStringTwo);

6. Empty Strings

Let’s now create three empty Strings:

String emptyLiteral = "";
String emptyNewString = new String("");
String emptyNewStringTwo = new String();

As we know by now, the emptyLiteral will be added to the String pool, while the other two go directly onto the heap.

Although these won’t be the same objects, all of them will have the same value:

assertFalse(emptyLiteral == emptyNewString)
assertFalse(emptyLiteral == emptyNewStringTwo)
assertFalse(emptyNewString == emptyNewStringTwo)
assertEquals(emptyLiteral, emptyNewString);
assertEquals(emptyNewString, emptyNewStringTwo);

7. null Values

Finally, let’s see how null Strings behave.

Let’s declare and initialize a null String:

String nullValue = null;

If we printed nullValue, we’d see the word “null”, as we previously saw. And, if we tried to invoke any methods on nullValue, we’d get a NullPointerException, as expected.

But, why does “null” is being printed? What is null actually?

Well, the JVM specification says that null is the default value for all references, so it’s not specifically tied to the String. And actually, the specification doesn’t mandate any concrete value encoding for null.

So, where is “null” coming from for printing a String then?

If we take a look at the PrintStream#println implementation, we’ll see it calls String#valueOf:

public void println(Object x) {
    String s = String.valueOf(x);
    synchronized (this) {
        print(s);
        newLine();
    }
}

And, if we look at String#valueOf, we get our answer:

public static String valueOf(Object obj) {
    return (obj == null) ? "null" : obj.toString();
}

And, obviously, that’s the reason for “null”.

8. Conclusion

In this article, we explored String initialization. We explained the difference between declaration and initialization. We also touched on using new and using the literal syntax.

Finally, we took a look at what it means to assign a null value to a String, how the null value is represented in memory, and how it looks when we print it.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW