How does Java distinguish these multiple methods with the same name/signature?

喜欢而已 提交于 2019-12-04 19:23:15

问题


I was tracking down a bug today, and I noticed something strange within one of our classes. I cut out as much code as possible to post here:

class A {
    static int obtainNumber() { return 42; }
    static int obtainNumber() { return 3; }
    static int obtainNumber() { return -1; }
    static {
        System.out.println(obtainNumber());
    }
}

This class has 3 methods with the exact same name and signature. At first I thought this was invalid code, but then eclipse would have highlighted the code in red. It does work:

javac A.java && java A
42
Exception in thread "main" java.lang.NoSuchMethodError: main

So I figured maybe Java will just use the first one it sees. I reordered to test:

class A {
    static int obtainNumber() { return 3; }
    static int obtainNumber() { return -1; }
    static int obtainNumber() { return 42; }
    static {
        System.out.println(obtainNumber());
    }
}

Nope, same result:

javac A.java && java A
42
Exception in thread "main" java.lang.NoSuchMethodError: main

I thought perhaps it uses the one with 42 because its the biggest. To test this, I took the original and changed the return values:

class A {
    static int obtainNumber() { return 0; }
    static int obtainNumber() { return 1; }
    static int obtainNumber() { return 2; }
    static {
        System.out.println(obtainNumber());
    }
}

It still knows to use the first one:

javac A.java && java A
0
Exception in thread "main" java.lang.NoSuchMethodError: main

And if I reorder them again:

class A {
    static int obtainNumber() { return 1; }
    static int obtainNumber() { return 0; }
    static int obtainNumber() { return 2; }
    static {
        System.out.println(obtainNumber());
    }
}

Same result:

javac A.java && java A
0
Exception in thread "main" java.lang.NoSuchMethodError: main

I thought Java was a text based language, which I'd expect makes this sort of thing impossible. How is Java tracking which method is which?


回答1:


Hidden characters. The source code is equivalent to

static int obtainNumber() { return 42; }

static int obtain\ufeffNumber() { return 3; }

static int obtain\ufeff\ufeffNumber() { return -1; }

To avoid this kind of problems, my source files are strictly US-ASCII. I want to be certain that the characters I see are exactly the characters compiler sees.




回答2:


I just copied/pasted this in my IDE and though it was a miracle, then when trying to save the file an error message appeared clearing this issue:

Save could not be complete. Try File > Save As... if the problem persists.

Reason: Some characters cannot be mapped using "Cp1252" caracter encoding. Either change the encoding or remove the characters which are not supported by the "Cp1252" caracter encoding.

So, these methods don't have the same name, just use characters that look the same.

More info related to character encoding on Java source files:

  • Java: a rough guide to character encoding (by McDowell)
  • How to use Special Chars in Java/Eclipse



回答3:


My original comment:

If you notice, the one that is taken is the one with the different syntax hiliting from the others. Perhaps that may clue you in based on hidden characters or grammar quirks.

I pasted this into Eclipse, and noticed a little extra character. I threw my saved file(in CP1252) into a hex editor, and found a byte order mark.

When I looked, CP1252 doesn't have a byte order mark, yet the characters are themselves in CP1252. A stray unicode character may have entered.

When I looked more closely, another method had another byte order mark in another byte order.

How they came to be, we'll never know. However, we do know that the compiler is taking the one without the byte order mark.

You should check the project for encoding issues, especially if it has been recovered from other system types or older storage media that may be faulty or have been exposed to legacy equipment.

We really need to deal with this FGITW behavior via meta sometime now.



来源:https://stackoverflow.com/questions/17662815/how-does-java-distinguish-these-multiple-methods-with-the-same-name-signature

工具导航Map