What is the purpose of the expression “new String(…)” in Java?

后端 未结 9 1066
迷失自我
迷失自我 2020-11-22 02:39

While looking at online code samples, I have sometimes come across an assignment of a String constant to a String object via the use of the new operator.

For example

相关标签:
9条回答
  • 2020-11-22 03:00

    Well, that depends on what the "..." is in the example. If it's a StringBuffer, for example, or a byte array, or something, you'll get a String constructed from the data you're passing.

    But if it's just another String, as in new String("Hello World!"), then it should be replaced by simply "Hello World!", in all cases. Strings are immutable, so cloning one serves no purpose -- it's just more verbose and less efficient to create a new String object just to serve as a duplicate of an existing String (whether it be a literal or another String variable you already have).

    In fact, Effective Java (which I highly recommend) uses exactly this as one of its examples of "Avoid creating unnecessary objects":


    As an extreme example of what not to do, consider this statement:

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

    (Effective Java, Second Edition)

    0 讨论(0)
  • 2020-11-22 03:01

    Generally, this indicates someone who isn't comfortable with the new-fashioned C++ style of declaring when initialized.

    Back in the C days, it wasn't considered good form to define auto variables in an inner scope; C++ eliminated the parser restriction, and Java extended that.

    So you see code that has

    int q;
    for(q=0;q<MAX;q++){
        String s;
        int ix;
        // other stuff
        s = new String("Hello, there!");
        // do something with s
    }
    

    In the extreme case, all the declarations may be at the top of a function, and not in enclosed scopes like the for loop here.

    IN general, though, the effect of this is to cause a String ctor to be called once, and the resulting String thrown away. (The desire to avoid this is just what led Stroustrup to allow declarations anywhere in the code.) So you are correct that it's unnecessary and bad style at best, and possibly actually bad.

    0 讨论(0)
  • 2020-11-22 03:09

    The sole utility for this constructor described by Software Monkey and Ruggs seems to have disappeared from JDK7. There is no longer an offset field in class String, and substring always use

    Arrays.copyOfRange(char[] original, int from, int to) 
    

    to trim the char array for the copy.

    0 讨论(0)
  • 2020-11-22 03:10

    String s1="foo"; literal will go in StringPool and s1 will refer.

    String s2="foo"; this time it will check "foo" literal is already available in StringPool or not as now it exist so s2 will refer the same literal.

    String s3=new String("foo"); "foo" literal will be created in StringPool first then through string arg constructor String Object will be created i.e "foo" in the heap due to object creation through new operator then s3 will refer it.

    String s4=new String("foo"); same as s3

    so System.out.println(s1==s2); //true due to literal comparison.

    and System.out.println(s3==s4);// false due to object comparison(s3 and s4 is created at different places in heap)

    0 讨论(0)
  • 2020-11-22 03:14

    The one place where you may think you want new String(String) is to force a distinct copy of the internal character array, as in

    small=new String(huge.substring(10,20))
    

    However, this behavior is unfortunately undocumented and implementation dependent.

    I have been burned by this when reading large files (some up to 20 MiB) into a String and carving it into lines after the fact. I ended up with all the strings for the lines referencing the char[] consisting of entire file. Unfortunately, that unintentionally kept a reference to the entire array for the few lines I held on to for a longer time than processing the file - I was forced to use new String() to work around it, since processing 20,000 files very quickly consumed huge amounts of RAM.

    The only implementation agnostic way to do this is:

    small=new String(huge.substring(10,20).toCharArray());
    

    This unfortunately must copy the array twice, once for toCharArray() and once in the String constructor.

    There needs to be a documented way to get a new String by copying the chars of an existing one; or the documentation of String(String) needs to be improved to make it more explicit (there is an implication there, but it's rather vague and open to interpretation).

    Pitfall of Assuming what the Doc Doesn't State

    In response to the comments, which keep coming in, observe what the Apache Harmony implementation of new String() was:

    public String(String string) {
        value = string.value;
        offset = string.offset;
        count = string.count;
    }
    

    That's right, no copy of the underlying array there. And yet, it still conforms to the (Java 7) String documentation, in that it:

    Initializes a newly created 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 original is needed, use of this constructor is unnecessary since Strings are immutable.

    The salient piece being "copy of the argument string"; it does not say "copy of the argument string and the underlying character array supporting the string".

    Be careful that you program to the documentation and not one implementation.

    0 讨论(0)
  • 2020-11-22 03:18

    Here is a quote from the book Effective Java Third Edition (Item 17: Minimize Mutability):

    A consequence of the fact that immutable objects can be shared freely is that you never have to make defensive copies of them (Item 50). In fact, you never have to make any copies at all because the copies would be forever equivalent to the originals. Therefore, you need not and should not provide a clone method or copy constructor (Item 13) on an immutable class. This was not well understood in the early days of the Java platform, so the String class does have a copy constructor, but it should rarely, if ever, be used.

    So It was a wrong decision by Java, since String class is immutable they should not have provided copy constructor for this class, in cases you want to do costly operation on immutable classes, you can use public mutable companion classes which are StringBuilder and StringBuffer in case of String.

    0 讨论(0)
提交回复
热议问题