0

I saw following member in java.lang.String :

private final char value[];

My question is:

Will following statement make a copy of the literal string into char[] metioned above, while another copy of literal string also exists in the constant pool.

String b1 = new String("abc");

If so, didn't the constant pool meaning less? Or we should prevent use new() to create String with literal?


@Update:

So, according to the answers, why String class need a char value[] variable inside it, why not just refer to a single copy inside constant pool? If create a string with new String("..."), won't the literal be put into constant pool if not exists in pool yet?

According to my imagine, the only benefit to use the new String() is that, it might be quicker than query a constant pool; or the constant pool has size limit, it will remove old constant value when size not enough? But I am not sure is this the way it works.


Conclusion

So, according to answers the new String() should only be used by constant pool maintainer itself, we programmers don't.

Eric
  • 22,183
  • 20
  • 145
  • 196
  • Why would you *ever* create a new string from an existing one? Strings are immutable. It's useless to do it. – JB Nizet Feb 13 '15 at 07:44
  • You should avoid using new String() creation – Gaskoin Feb 13 '15 at 07:48
  • What is meaningless is creating a `String` with `new String("abc")`. It is never necessary to create a new `String` object from a string literal like `"abc"` in this way. Never write code like this. – Jesper Feb 13 '15 at 07:55
  • Is this JVM-dependent? I could see one JVM not honoring `new` and using a String cache, while another doesn't. – David Ehrmann Feb 13 '15 at 07:55
  • @DavidEhrmann the JLS requires that `new String("abc") != "abc"` – user253751 Feb 13 '15 at 07:57
  • @JBNizet I don't use new much, but the what there need a `char []` member inside String in that case. – Eric Feb 13 '15 at 08:10
  • @EricWang whare would you store the characters of a String, if not in a char array? – JB Nizet Feb 13 '15 at 08:12
  • @JBNizet Create one in constant pool, and refer to it. Ok, so just avoid use new() ? – Eric Feb 13 '15 at 08:13
  • So ALL the strings would be in the constant pool? That would be insane. Most strings used in a program are temporary. – JB Nizet Feb 13 '15 at 08:14
  • @JBNizet When create string with new(), won't it be put into constant pool? – Eric Feb 13 '15 at 08:15
  • @JBNizet Ok, I get it, the constant pool itself will need the `char value[]`. Thx. – Eric Feb 13 '15 at 08:32

3 Answers3

2

Latter is right. To create a String instance from literal using new() is absolutely meaningless.

yanana
  • 2,241
  • 2
  • 18
  • 28
  • 1
    If you're passed the String as a parameter to a constructor and are going to synchronize on it locally, it very much has meaning. I've seen this done with primitive wrappers. But it's still a bad idea. If you need a lock, use a lock. – David Ehrmann Feb 13 '15 at 07:57
2

You can use new, but it will be a little tricky that you specify using the "intern" method of String. like this:

String a = "ABC";
String b = new String("ABC").intern();
System.out.println(a == b);

Output is true, if no "intern", then it is a copy from the constant pool.

String a = "ABC";
String b = new String("ABC");
System.out.println(a == b);

output is false. If you looks into the prototype of this String constructor, it shows that:

/**
     * Initializes a newly created {@code String} object so that it represents
     * the same sequence of characters as the argument; in other words, the
     * newly created string is a copy of the argument string. Unless an
     * explicit copy of {@code original} is needed, use of this constructor is
     * unnecessary since Strings are immutable.
     *
     * @param  original
     *         A {@code String}
     */
    public String(String original) {
        this.value = original.value;
        this.hash = original.hash;
    }
Cui Heng
  • 1,265
  • 8
  • 10
  • Ok, so it's not necessary to use `new String()` in any case, right? – Eric Feb 13 '15 at 08:25
  • Yes, if you don't have strong reason, avoid using new String for literal is the smart choice, as reference to const pool would speed your program performance in some cases, such as using the "String" as the key of a map. – Cui Heng Feb 13 '15 at 08:27
1

String in java are like any other programming language, a sequence of characters. This is more like a utility class to work on that char sequence. This char sequence is maintained in following variable:

/** The value is used for character storage. */
private final char value[];

When you create the String with new keyword like this

String b1 = new String("abc");

then Object is created into the Heap Memory , and when Java Compiler encounters any String literal , It creates one Object inside constant pool

Now b1 is pointing to the Object inside Heap Memory , and since a String literal is also there so an Object also created in constant pool to whom no one is pointing

As Effective java 2nd Edition Says

String s = new String("neerajjain");  //DON'T DO THIS!

because you are unnecessarily creating 2 objects when work can be done by only 1 Object .

but there are some cases where you might use new String("string") you can find them here

Community
  • 1
  • 1
Neeraj Jain
  • 7,643
  • 6
  • 34
  • 62