Java中的substring()错误

时间:2015-03-08 03:24:14

标签: java

当我遇到错误时,我正在测试Java中的子字符串功能。虽然子字符串的值是正确的,但它在if语句中失败。这是我的代码:

public static void main(String[] args) {
    String foo="bar";
    String subFoo=foo.substring(0,1);
    System.out.println(foo);
    System.out.println(subFoo);
    if (subFoo=="b") {
        System.out.println("its b");
    } else if (subFoo!="b") {
        System.out.println("its not b");
    } else {
        System.out.println("who knows?");
    }

}

应该输出行

bar
b
its b

运行时,而是打印

bar
b
its not b

即使它正确打印subFoo为" b"它似乎没有在if语句中正确注册。有什么想法吗?

0 个答案:

没有答案