1

I've got the following code which works except for the command line arguments, everytime i write "Insertion" it won't go in the if-statement so the output would be "Algorithm not found. Use: [ Insertion | Merge ]"

  public static void main(String[] args) throws IOException, InsertionAndMergeException, Exception {
    if( args.length < 2 ) {
      System.out.println("Use: <path> <algorithm> ");
      System.exit(1);
    }

    if(args[1] == "Insertion" || args[1] == "Merge"){

      testWithComparisonFunction(args[0], args[1], new RecordComparatorIntField());

    }else
      System.out.println("Algorithm not found. Use: [ Insertion | Merge ]");
  }  

In command-line i'm typing this, what am I doing wrong?

java insertionandmergeusagejava/InsertionAndMer
geUsage "/home/zenoraiser/Scrivania/Università/Secondo Anno/Algoritmi/1718/LAB/Progetto/Integers.txt" "Insertion"
Zeno Raiser
  • 207
  • 2
  • 10
  • you are comparing your Strings wrong. == is a referntial comparison, you need a comparison by value, so use the equals method instead. Just change this: if(args[1] == "Insertion" || args[1] == "Merge") by this: if ( "Insertion".equals(args[1]) || "Merge".equals(args[1])) – Stultuske Apr 03 '18 at 11:39
  • 1
    Possible duplicate of [How do I compare strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – XtremeBaumer Apr 03 '18 at 11:39
  • replace `System.exit(1);` with `return;` – Andrew Tobilko Apr 03 '18 at 11:40

1 Answers1

5

You're confusing == with .equals, if you change your if statement to

if ("Insertion".equals(args[1]) || "Merge".equals(args[1])) {

You should get the expected result.

In Java, the == operation takes the LHS value and compares it directly with the RHS value, this is fine for primitive types such as int, double, etc. Strings are a bit different though. Because a String is effectively an array of characters, it is stored as an Object, so the == operator will compare the pointers to the LHS/RHS (which in this case are not equal).

You can observe seemingly strange behavior around this with code like:

String a = "Test.";
String b = "Test.";
System.out.println(a == b); // true
System.out.println(a.toLowerCase() == b.toLowerCase()); // false

This is due to a process known as "String interning", which effectively stores multiple strings under the same pointer while they have the same value.

Also note that by putting the String literal first in the comparison, you remove the possibility of a NullPointerException if args[1] were to be non-existent.

A. Bandtock
  • 1,233
  • 8
  • 14
  • you mean 'String literal', instead of String constant – Stultuske Apr 03 '18 at 11:45
  • The inclusion of the `String` interning is a great addition. More information on this can be found at JLS section 3.10.5: https://docs.oracle.com/javase/specs/jls/se10/html/jls-3.html#jls-3.10.5. As quoted from the JLS: "The result of explicitly interning a computed string is the same string as any pre-existing literal string with the same contents." – Justin Albano Apr 03 '18 at 11:59