I was testing out the substring capabilities in Java when I ran into an error. Although the substring's value is correct, it fails in an if statement. Here is my code:
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?");
}
}
It is supposed to output the lines
bar
b
its b
when run, but instead prints
bar
b
its not b
Even though it prints subFoo correctly as "b" it doesn't seem to register properly in the if statement. Any ideas?