When I pass an argument to my Main method via the BlueJ entry box, it works fine. When I pass it as a command-line parameter, it has problems.
- It can be printed out correctly from args[0]
- It is type String as expected
- args.length > 0 is correctly "true"
- it is not correctly recognized by if(args[0] == "myArg")
Simplified code:
command-line: java src.main.MyClass myArg
public static void main(String[] args)
{
System.out.println(args[0]); //prints "myArg" correctly
if (args.length == 0) {
throw new IllegalArgumentException();
}
else if (args.length > 0) {
System.out.println("made it to > 0"); //prints fine
if(args[0] == "myArg") { //does not correctly eval "true"
System.out.println("evaluated correctly"); // does not print
}
}
Again, using the BlueJ argument entry, the argument functions correctly, but from the command line, it is read correctly, enters my method correctly, but somehow is not being correctly evaluated as "myArg"
Answered
by Juan, below.
Humbling mistake of using == instead of .equals() to compare strings. I am not sure why this incorrect code would still work when passing the argument via the BlueJ interface, but on to more important issues. Thank you stackoverflow.