1 2 Previous Next 18 Replies Latest reply: Jun 4, 2007 4:48 PM by 807606 RSS

    Strings not equal if passed in from the command line?


      I have the following class which compares two strings...

      The first if statement simply compares the string test to the literal "alan". This test is true.

      The second if statement takes a user entered parameter from the command line and performs the same equality test, however if "alan" is entered as a parameter in the command line the equality test fails???

      My question is why does the equality test work in the first if statement and not in the second if statement, surely it is the same test??
      class Test
          public static void main(String[] args)
              String test = "alan";
              if(test == "alan")
                  System.out.println("Strings equal using == operator");
              if( args[0] == "alan" )
                   // This never Prints
                  System.out.println(" Strings equal using args[0] == alan");
      note : I have also tried using
      but this doesn't work either!

        1 2 Previous Next