gpt4 book ai didi

java - 使用 substring() 看似不等价的字符串

转载 作者:行者123 更新时间:2023-11-29 09:39:46 25 4
gpt4 key购买 nike

<分区>

Possible Duplicate:
Java String.equals versus ==

我是 Java 的新手,所以不要指望事情会很简单,但这让我完全震惊了。为什么 if 语句不能正确求值:

import javax.swing.JFrame;
import javax.swing.JTextArea;

public class StringMystery extends JFrame {

JTextArea txt = new JTextArea();
String string1 = "abcde";
String string2 = string1.substring(0,4);

public StringMystery() {

setLayout(null);

txt.setBounds(20, 20, 100, 100);
add(txt);

txt.setText(string2);

//string 2 is definitely equal to "abcd" but this doesn't evaluate:
if (string2 == "abcd"){
txt.setText("string2 = abcd");
}

}


public static void main(String[] args) {

StringMystery jtxt = new StringMystery();
jtxt.setSize(200,200);
jtxt.setTitle("String Mystery");

jtxt.setVisible(true);

}
}

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com