Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 852
Dog Avatar asked Jul 15 '13 20:07

Dog


People also ask

Can you have two methods with the same name in Java?

For convenience, Java allows you to write more than one method in the same class definition with the same name. For example, you can have two methods in ShoppingCart class named computeCost. Having two or more methods named the same in the same class is called overloading.

Can a Java class have many methods with the same signature?

You cannot declare more than one method with the same name and the same number and type of arguments, because the compiler cannot tell them apart. The compiler does not consider return type when differentiating methods, so you cannot declare two methods with the same signature even if they have a different return type.

Can two methods have the same signature?

Two Methods cannot have same method signature. Methods can have same method name, and this process called method overloading.

Can we have two methods in a class with the same name but different return type?

We can not define more than one method with the same name, Order, and type of the arguments. It would be a compiler error. The compiler does not consider the return type while differentiating the overloaded method. But you cannot declare two methods with the same signature and different return types.


2 Answers

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
like image 94
Luiggi Mendoza Avatar answered Oct 24 '22 18:10

Luiggi Mendoza


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.

like image 27
ZhongYu Avatar answered Oct 24 '22 19:10

ZhongYu