System.out.println(args[0] == "x");
System.out.println(args[0].equals("x"));
String[] name = {"x"};
System.out.println(name[0] == "x");
System.out.println(name[0].equals("x"));
String[] hello = new String[1];
hello[0] = "x";
System.out.println(hello[0] == "x");
System.out.println(hello[0].equals("x"));
results in
false
true
true
true
true
true
Why does the first test result in 'false' when I invoke "java classname x"?
xargument fromjava YourClass xis not placed in String pool. You can get same effect withString[] name = {new String("x")};==compares object addresses. The first two Strings you're comparing are different objects with the same contents, therefore==reports them as being different. String literals (that is, characters between pairs of"symbols), on the other hand, are always the same object if they have the same value, so comparing"x" == "x"will always reporttrue.