如何创建新字符串()和普通字符串?java中的字符串类(混淆)

如何创建新字符串()和普通字符串?java中的字符串类(混淆),java,string,Java,String,我在读java中的字符串,并试图理解它 起初,字符串s1=“11”和字符串s2=新字符串(“11”)的工作原理(创建)很简单,我也理解intern方法。 但我遇到了这个例子(由一位朋友给出),让我对一切都感到困惑 我需要帮助来理解这一点 String s1 = new String(new String("2")+new String("2")); s1.intern(); String s2="22"; System.out.print(s1==s2); //=>true as out

我在读java中的字符串,并试图理解它

起初,字符串s1=“11”和字符串s2=新字符串(“11”)的工作原理(创建)很简单,我也理解intern方法。
但我遇到了这个例子(由一位朋友给出),让我对一切都感到困惑

我需要帮助来理解这一点

String s1 = new String(new String("2")+new String("2"));
s1.intern();
String s2="22";
System.out.print(s1==s2);  //=>true as output.
String s3 =new String (new String("2")+new String("2"));
s3.intern();
String s4="22";
System.out.print(s3==s4);  //=>false as output.
这个代码的答案是正确的和错误的

S1和s2的部分很好,根据我的理解是正确的,但第二部分我不理解


希望有人能逐行破解代码,帮助我理解

在java中,object1==object2表示

object1和object2在内存中的地址是否相同

object1.等于(object2)

意味着它们是否相等,例如,所有字段的值是否相同

对于两个字符串S1和S2, string1.equals(S2)表示,它们在相同的序列中有相同的字符吗?

S1==S1表示
string1和string2在内存中是否存储在同一地址?
在java中object1==object2表示

object1和object2在内存中的地址是否相同

object1.等于(object2)

意味着它们是否相等,例如,所有字段的值是否相同

对于两个字符串S1和S2, string1.equals(S2)表示,它们在相同的序列中有相同的字符吗?

S1==S1表示
string1和string2是否存储在内存中的同一地址?
请注意,类型字符串是大写的,不是Java的8种基本类型(int、boolean、double、char等)之一。这表示字符串的任何实例都是使用类字符串的“蓝图”构建的对象。因为Java中引用对象的变量只存储实际对象存储的内存地址,所以当您将字符串与==进行比较时,它会比较内存位置

String str1 = new String("hello");
String str2 = str1; //sets str1 and str2 pointing to same memory loc
if (str1 == str2){
    //do stuff; the code will enter this if-statement in this case
}
Java中比较对象内值的方法是使用equals(),例如:

String str1 = new String("hello");
String str2 = new String("hello"); //str2 not same memory loc as str1
if (str1.equals(str2)){
    //do stuff; the code will enter this if-statement in this case
}
这对于初学者来说是一个常见错误,因为基本类型不是对象,您可以比较两个整数是否相等,例如:

int one = 1; //primitive types are NOT objects
int two = 2; //notice when I make an int, I don't have to say "new"
             //which means a new **object**
if (int1 == int2) {
    //do stuff; in this case the program will not enter this if-statement
}
你似乎什么都懂,除了最后一行的意思。见最后一行我的评论

String s1 = new String(new String("2")+new String("2")); //declare AND initialize s1 as a new String object
s1.intern();
String s2="22"; //declare a new variable s2 and point it to the same object that s1 is pointing to
System.out.print(s1==s2);
String s3 =new String (new String("2")+new String("2"));
s3.intern();
String s4="22";
System.out.print(s3==s4); //check if s3 and s4 are stored in the same memory location = FALSE

请注意,类型字符串是大写的,不是Java的8种基本类型(int、boolean、double、char等)之一。这表示字符串的任何实例都是使用类字符串的“蓝图”构建的对象。因为Java中引用对象的变量只存储实际对象存储的内存地址,所以当您将字符串与==进行比较时,它会比较内存位置

String str1 = new String("hello");
String str2 = str1; //sets str1 and str2 pointing to same memory loc
if (str1 == str2){
    //do stuff; the code will enter this if-statement in this case
}
Java中比较对象内值的方法是使用equals(),例如:

String str1 = new String("hello");
String str2 = new String("hello"); //str2 not same memory loc as str1
if (str1.equals(str2)){
    //do stuff; the code will enter this if-statement in this case
}
这对于初学者来说是一个常见错误,因为基本类型不是对象,您可以比较两个整数是否相等,例如:

int one = 1; //primitive types are NOT objects
int two = 2; //notice when I make an int, I don't have to say "new"
             //which means a new **object**
if (int1 == int2) {
    //do stuff; in this case the program will not enter this if-statement
}
你似乎什么都懂,除了最后一行的意思。见最后一行我的评论

String s1 = new String(new String("2")+new String("2")); //declare AND initialize s1 as a new String object
s1.intern();
String s2="22"; //declare a new variable s2 and point it to the same object that s1 is pointing to
System.out.print(s1==s2);
String s3 =new String (new String("2")+new String("2"));
s3.intern();
String s4="22";
System.out.print(s3==s4); //check if s3 and s4 are stored in the same memory location = FALSE

java中存在堆和堆栈, 堆是保存所有对象的地方 堆栈是保存变量的地方 现在还存在另一种字符串和整数(数字)列表

正如您所知,可以通过以下方式创建字符串

像new String(“word”)或just=“word”当您使用第一种方法创建新对象(heap)时,当您使用另一种方法时,您将单词保存在一个单词堆栈中(Java Engeniers认为如果您不创建manny对象或重复单词会更好,因此他们为单词创建了一个特殊的堆栈,对于0到127的整数也是如此)正如我说的,你必须知道有一个堆栈和一个堆,看看这个例子

String wordOne ="hola";
        String wordTwo = "hola";
        String wordTres = "hola";
        System.out.println(wordOne == wordTwo);
        System.out.println(wordTres == wordTwo);
        System.out.println(wordOne == wordTres);

        String wordFour = new String("hola");
        System.out.println(wordOne == wordFour);

        Integer uno = 127;
        Integer dos = 127;
        System.out.println(uno == uno);
        Integer tres = 128;
        Integer cuatro = 128;
        System.out.println(tres == cuatro);
字符串x=“word”;保存在一个特殊的堆栈中 字符串y=新字符串(“不是”)

但我记不太清楚堆栈的规则,但无论如何,我建议您使用wordX.equals(wordY)比较所有单词

也可以使用==从0到127比较对象中的数字,但如果使用对象使用相等,则相同。尽管使用数字有更好的方法,但尽管使用相等,将一个数字转换为基本值(内存会更好)


java中存在堆和堆栈, 堆是保存所有对象的地方 堆栈是保存变量的地方 现在还存在另一种字符串和整数(数字)列表

正如您所知,可以通过以下方式创建字符串

像new String(“word”)或just=“word”当您使用第一种方法创建新对象(heap)时,当您使用另一种方法时,您将单词保存在一个单词堆栈中(Java Engeniers认为如果您不创建manny对象或重复单词会更好,因此他们为单词创建了一个特殊的堆栈,对于0到127的整数也是如此)正如我说的,你必须知道有一个堆栈和一个堆,看看这个例子

String wordOne ="hola";
        String wordTwo = "hola";
        String wordTres = "hola";
        System.out.println(wordOne == wordTwo);
        System.out.println(wordTres == wordTwo);
        System.out.println(wordOne == wordTres);

        String wordFour = new String("hola");
        System.out.println(wordOne == wordFour);

        Integer uno = 127;
        Integer dos = 127;
        System.out.println(uno == uno);
        Integer tres = 128;
        Integer cuatro = 128;
        System.out.println(tres == cuatro);
字符串x=“word”;保存在一个特殊的堆栈中 字符串y=新字符串(“不是”)

但我记不太清楚堆栈的规则,但无论如何,我建议您使用wordX.equals(wordY)比较所有单词

也可以使用==从0到127比较对象中的数字,但如果使用对象使用相等,则相同。尽管使用数字有更好的方法,但尽管使用相等,将一个数字转换为基本值(内存会更好)

s1.intern()
s1
添加到字符串池中,因此字符串
“22”
现在位于字符串池中。因此,当您编写
s2=“22”
时,这与s1是相同的
“22”
,因此
s1==s2

s3.intern()<