String Initialization in Java

String Initialization in Java

Learn about the difference between declaration and initialization of String in Java.

Learn about the difference between declaration and initialization of String in Java.

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](https://www.baeldung.com/java-string "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 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.

All code samples used in the article are available over on Github.

java string

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

How to Install OpenJDK 11 on CentOS 8

What is OpenJDK? OpenJDk or Open Java Development Kit is a free, open-source framework of the Java Platform, Standard Edition (or Java SE).

Java String Class Example | String Class in Java Tutorial

Java String is an inbuilt class that deals with all that is written in double quotation marks, or other words string is a collection of characters.

Java String Programs For Beginners | Java String Examples | Java Training

Java String Programs will talk about top 10 important programs in java strings with examples.

Java String length() | How To Find String Length In Java

Java String length(). Java provides a in-built java.lang.String.length() method. Length of a String is essentially the number of 16-bit Unicode characters.

Java String – String Functions In Java With Examples

In this post, we will be discussing about a new concept, Java String. String is a sequence of characters. But in Java, a string is an object that represents a sequence of characters. The java.lang.String class is used to create string object.