可能性の重複:
Java String.equals versus ==文字列一見等価ではない使用して、サブストリング()
私は物事が簡単であることを期待していないのJavaに新しいですが、これは私は完全にショックを受けシェルあり。 はなぜ地球上の文が正しく評価されない場合:
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);
}
}
このスレッドを見つけた私のような初心者のために、他の百万の文字列==質問 – Qwerky