下面这两种说法有什么不同?

String s = "text";

String s = new String("text");

当前回答

当你使用一些东西,如new String(“Hello World”),SpotBugs代码分析工具,抱怨一个性能问题。

这个问题的描述显示了新字符串和常量字符串的区别

使用java.lang.String(String)构造函数会浪费内存,因为 这样构造的对象将在功能上难以区分 作为参数传递的字符串。只需使用参数String 直接。 错误类型和模式:Dm - DM_STRING_CTOR

其他回答

把“bla”想象成一个魔法工厂,就像Strings.createString(“bla”)(pseudo)。工厂保存了所有以这种方式创建的字符串池。

如果它被调用,它会检查池中是否已经有这个值的字符串。如果为真,则返回此string对象,因此以这种方式获得的字符串确实是同一个对象。

如果不是,它在内部创建一个新的字符串对象,将其保存在池中,然后返回它。因此,当下次查询相同的字符串值时,它将返回相同的实例。

手动创建新的String("")通过绕过字符串字面量池来覆盖这种行为。因此,应该始终使用equals()检查相等性,它比较字符序列,而不是对象引用相等性。

一个简单的理解差异的方法如下:-

String s ="abc";
String s1= "abc";
String s2=new String("abc");

        if(s==s1){
            System.out.println("s==s1 is true");
        }else{
            System.out.println("s==s1 is false");
        }
        if(s==s2){
            System.out.println("s==s2 is true");
        }else{
            System.out.println("s==s2 is false");
        }

输出是

s==s1 is true
s==s2 is false

因此new String()总是会创建一个新实例。

当您将字符串存储为

String string1 = "Hello";

然后JVM在称为String常量池的单独内存块中创建一个具有给定价格的String对象。

当我们有尝试生成另一个String的倾向时

String string2 = "Hello";

JVM验证String常量池中是否存在价格恒定的String对象,如果存在,JVM不会创建一个全新的对象,而是将现有对象的引用赋值给新变量。

当我们存储String为

String string = new String("Hello");

使用new关键字,不管String常量池的内容是什么,都会生成一个具有给定价格的全新对象。

一个是在字符串常量池中创建一个字符串

String s = "text";

另一个在常量池中创建一个字符串("text"),在普通堆空间中创建另一个字符串(s)。两个字符串的值相同,即"text"。

String s = new String("text");

如果以后未使用,则会丢失(符合GC的条件)。

另一方面,字符串字面值被重用。如果你在你的类的多个地方使用“text”,它实际上将是一个且只有一个String(即在池中多次引用同一个字符串)。

抱歉回复晚了,但我急需回复。 首先,我们需要知道一些Java.lang.String类规则。

String Literals e.g.String str="java"; (we use only double Quotes) are different from String Object (we use new keyword) e.g. String str=new String("java"); String is Immutable Object i.e. If value changes a new Object is created and returned to you eg See replace() and replaceAll() functions and many more. This creates a problem of many String Object in Modification, So creators of Java came up an Idea was called StringPool. StringPool is stored in heap area where object reference data will be stored as we know String is Char[](before java 9 very Long to read) or byte[](after java 9 short to read). String literals are stored in StringPool and String Objects are stored in as usual heap Object Area. If there are many Object String Initialization JVM heap will be finished in String Operations only, Java Development team came up with intern() solution this moves/changes memory reference to StringPool. Program: Comparing String references to objects

另一个更好地理解java.lang.String的好链接

import java.util.*; 

class GFG { 
    public static void main(String[] args) 
    { 
      String siteName1 = "java.com";
        String siteName2 = "java.com";
        String siteName3 = new String("java.com");
        String siteName4 = new String("java.com").intern();
      
    System.out.println("siteName1:::"+Integer.toHexString(System.identityHashCode(siteName1)));
      System.out.println("siteName2:::"+Integer.toHexString(System.identityHashCode(siteName2)));
      System.out.println("siteName3 creation Of New Object Without Interned:::"+Integer.toHexString(System.identityHashCode(siteName3)));//must be Diffrent bcoz new Object In Heap Area
      System.out.println("siteName4 creation Of New Object With Interned:::"+Integer.toHexString(System.identityHashCode(siteName4)));//must be same MemoryAddress of siteName1,siteName2 and Interned, bcoz Objects Points to String pool Now
      
      System.out.println(siteName1 == siteName2); // true
      System.out.println(siteName1 == siteName3); // false this tells about lietral vs String Objects
      String siteName5 = siteName3.intern(); // Interning will not change Original Object but gives us a new Object
      
      System.out.println("siteName5 Interned from siteName3:::"+Integer.toHexString(System.identityHashCode(siteName5)));//must be same MemoryAddress of siteName1,siteName2 and Interned, bcoz Objects Points to String pool Now
      
      System.out.println(siteName1 == siteName3); // false this tells about Immutability
      System.out.println(siteName1 == siteName5); // true After Intering both are same
      System.out.println(siteName1 == siteName4); // true
      System.out.println(siteName5 == siteName4); // true
    } 
}