java - What is the difference between "text" and new String("text")? -


What is the difference between these two statements?

  string s = "text"; String s = new string ("text");  

new string ("text"); clearly creates a string specific example from a new and referenced form of the object; string s = "text"; string continuous pool can reuse an example if someone is available.

You will want to use very little any time new string (other string) constructor from the API:

: Launches an newly created string object, so that it represents the same sequence of characters as the argument; In other words, the newly created string argument is a copy of the string. A clear copy of the original is not required, unless the wire is irreversible, the use of this manufacturer is unnecessary.

related question

  • what does the difference mean? H3>

    Check the following snippet:

      string s1 = "foobar"; String s2 = "foobar"; System.out.println (s1 == s2); // true s2 = new string ("focus"); System.out.println (s1 == s2); // false system Out.prints (s1.equals (s2)); // True  

    == A reference identifier is two objects that are equals not necessary == . It is usually incorrect to use == on reference codes; Instead, most of the time, par needs to be used instead.

    However, if for any reason you have to create two equal , but = = string, you can Use string (other string) constructor. It should be said again, however, it is strange too , and rarely intended.

    Reference

    Related Issues


    Comments

    Popular posts from this blog

    windows - Heroku throws SQLITE3 Read only exception -

    lex - Building a lexical Analyzer in Java -

    python - rename keys in a dictionary -