You're comparing two string references for identity, not whether they refer to equal strings. It's not skipping the if
statement - it's evaluating the condition, and finding that it's false, so it's not going into the body. Try this:
if (input.equals("x"))
Or if input
might be null and you don't want that to cause an exception:
if ("x".equals(input))
This isn't just true of strings - whenever you have ==
, it will compare the values of the two expressions... and if those values are references, it simply compares whether those two references are equal, i.e. whether they refer to the same object. equals
, on the other hand, is applied polymorphically - so the object it's called on can determine what constitutes equality for that class.
As another example:
Integer x = new Integer(1000);
Integer y = new Integer(1000);
System.out.println(x == y); // false
System.out.println(x.equals(y)); // true