What is different here is this design:

String name = "Tom"; 

from this:

 String name = new String("Jerry"); 

I do not really understand what a link is.
Why do this?

 public void foo(){ new String("Моя строка"); } 

If you can do this:

 public void foo(){ String name = "Mike"; } 

Or

 public void foo(){ String name = new String("John"); } 

    2 answers 2

    String name = "Tom" for the first time creates a string literal in memory.
    Subsequent use of the same literal will cause the creation of a new link in the same place.
    String cat = "Tom" will use the same string, the addresses are the same.

    String name = new String("Jerry"); creates a new object in memory.
    If you make String mouse = new String("Jerry"); - create another object in memory. Addresses are different, i.e. the string "jerry" is stored twice.

    There is more subtlety. Comparison with "==" verifies the identity of addresses, and the String.equals method performs a comparison of the content. Therefore, name==mouse returns false - the addresses are different.

    This example:

     public void foo(){ new String("Моя строка"); } 

    doesn't make much sense. A link to a new object must be assigned somewhere. In languages ​​without garbage collection, such code will lead to memory leaks; in Java, a hanging object must be disposed of. For the rest of the examples, the above remains valid.

    I do not really understand what a link is.

    You have Kolyan on your phone and its number.
    His mom has a "son" and his number.
    At the head of the phone recorded "sysadmin" and his number.
    A friend recorded "eared zaya" and his number.
    Kolyan is one, and there may be several references to it.

    Kolyan resigned, the chief hired a new sysadmin. The link in his phone has become invalid (in Java, the collector may follow this, but in this context it is irrelevant).
    He hired a new sysadm = ("Петин номер"); and his link now points to a new object.
    And yours has not changed, he is still Kolyan.

    • I added some information, you can expand the answer, thanks in advance! - Petrovchenko Ivan
    • I would also add about the comparison period in terms of == there will be a comparison of the links and that for "Tom" it will be true as for equals, but for "Jerry" equality will be only in equals. - Ihar Hulevich
    • @Ihar Hulevich Yes, I have not reached this point yet - MBo
    • 3
      @MBo, the example with "Kolya" is cool!) - Ihar Hulevich

    String objects are basically wrappers around string literals. Unique string objects are combined to prevent unnecessary creation of objects, and the JVM may decide to combine string literals from the inside. There is also direct bytecode support for String constants that are referenced several times, providing the compiler with support for this.

    The last two examples are variables that store references to the object. In the 3rd example, you simply create an object, without reference. Therefore, you will not have access to it and it will most likely be removed by the garbage collector.

    Relevant excerpt from JLS 7 3.10.5 :

    In addition, a string literal always refers to the same instance of the String class. This is due to the fact that string literals, or, more generally, strings that are constant expression values ​​(§15.28), are "interned" to exchange unique instances using the String.intern method.

    Example:

      String hello = "Hello", lo = "lo"; System.out.print((hello == "Hello") + " "); System.out.print((Other.hello == hello) + " "); System.out.print((other.Other.hello == hello) + " "); System.out.print((hello == ("Hel"+"lo")) + " "); System.out.print((hello == ("Hel"+lo)) + " "); System.out.println(hello == ("Hel"+lo).intern()); 

    Displays true true true true false true .

    The Java programming language requires that identical string literals (that is, literals containing the same sequence of code points) must refer to the same String class instance (JLS §3.10.5). In addition, if the String.intern method String.intern called on any string, the result is a reference to the same class instance that will be returned if this string appeared as a literal. Thus, the following expression must be true : ("a" + "b" + "c").intern() == "abc" .

    Taken from here .