class StringEquality {
void ifChain(String s) {
if (s.equals("foo")) {
}
else if (s.equals("bar")) {
}else if("foo".equals(s)) {
}
}
void switchAfterIf(String s) {
if (s.equals("foo")) {
return;
}
switch(s) {
case "bar":
case "baz":
case "foo":
}
}
void lengths(String s, String s1) {
if(s.equals(s1) && s.length() != s1.length()) {}
if(s.length() != s1.length() && s.equals(s1)) {}
}
// IDEA-197195
void foo() {
String v = "Foo";
String vv = "FooFoo";
String vvv = vv.substring(3);
System.out.println(v.equals(vv));
System.out.println(v == vv);
System.out.println(v.equals(vvv));
System.out.println(v == vvv); // Unsure: strings are equal by content, but DFA does not know whether they are equal by reference
System.out.println(vv.equals(vvv));
System.out.println(vv == vvv);
}
boolean compare(Object a, Object b) {
if(a == b) {
if(a instanceof String) {
return ((String)a).equals(b);
}
return true;
}
if(a instanceof String && b instanceof String) {
return ((String)a).equals(b);
}
return false;
}
static final String SENTINEL = "foo";
void test(Object o) {
if(o == SENTINEL) {
System.out.println("oops");
} else {
System.out.println(((Number)o).longValue());
}
}
String internFoo(String s) {
if (s.equals("foo")) {
// "foo" is often used, intern it
s = "foo";
}
return s;
}
void length(String s) {
if(!s.startsWith("--") || s.equals(".")) {
System.out.println("invalid parameter");
}
}
interface X {
Object getY();
}
void testWithCanonicalization(Object obj) {
if(obj instanceof X) {
X x = (X)obj;
if (x.getY() instanceof String) {
System.out.println("oops");
}
}
}
void testObject() {
Object x = " foo ".trim();
Object y = " foo ".trim();
if (x == y) {}
}
void testIncorrect(String s) {
if(s == s.length()) {}
}
void testTrim() {
System.out.println(" EQ ".trim() == "EQ");
}
}