Home  >  Article  >  Java  >  Why is it necessary to use `new` when creating a `CaseInsensitiveString` object but not a `String` object?

Why is it necessary to use `new` when creating a `CaseInsensitiveString` object but not a `String` object?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-11-13 08:23:02762browse

Why is it necessary to use `new` when creating a `CaseInsensitiveString` object but not a `String` object?

Creating Strings in Java: The Case of "String s = new String("silly")"

When creating strings in Java, it's crucial to understand the behavior of the String class. Unlike many other classes in Java, String literals automatically create String objects without the need for the new keyword. However, creating a new String object using the new operator is explicitly discouraged, as it can lead to unnecessary memory consumption.

To illustrate this, consider the following code:

String s = "No longer silly";

In this case, the literal "No longer silly" is directly assigned to the s variable, resulting in a single String object being created. However, the following code snippet:

String s = new String("silly");

creates an unnecessary additional String object. To avoid this, the recommendation is to use literal assignment whenever possible, as seen in the first example.

However, there may be situations where it's necessary to create String objects dynamically. For instance, consider the following class:

public final class CaseInsensitiveString {
    private String s;
    
    public CaseInsensitiveString(String s) {
        if (s == null) {
            throw new NullPointerException();
        }
        this.s = s;
    }
}

In this scenario:

  1. Why is the first statement ok? Shouldn't it be CaseInsensitiveString cis = "Polish";?
    The first statement, CaseInsensitiveString cis = new CaseInsensitiveString("Polish");, is correct. While it's generally recommended to use literal assignment for String objects, this rule does not apply to objects of other classes, such as CaseInsensitiveString. In this case, the new keyword is necessary to create a new instance of the CaseInsensitiveString class.
  2. How do I make CaseInsensitiveString behave like String so the above statement is OK (with and without extending String)? What is it about String that makes it OK to just be able to pass it a literal like that? From my understanding there is no "copy constructor" concept in Java?
    The String class in Java has a unique behavior compared to other classes. Specifically, literals are implicitly converted to String objects, eliminating the need for explicit construction. This allows statements like String s = "Polish"; without invoking the new operator. There is no equivalent "copy constructor" concept in Java. Instead, the String class provides immutability and a predefined constructor that accepts a character sequence, such as a literal string, to create a new String object.

The above is the detailed content of Why is it necessary to use `new` when creating a `CaseInsensitiveString` object but not a `String` object?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn