Reputation: 359
There are two methods both are overloaded methods
int add(int a, int b)
And
int add(long a, long b)
And if I call
add(3,4)
Which method will be called?
Upvotes: 2
Views: 102
Reputation: 332
int add() will be called:
Method Overloading: If a class have multiple methods by same name but different parameters, it is known as Method Overloading. In java, Methood Overloading is not possible by changing the return type of the method..
See The output of this code : Code:
public class MethodOverload {
static void add(int a, int b) {
System.out.println("Call 1:" + (a + b));
}
static void add(Long a, Long b) {
System.out.println("Call 2:" + (a + b));
}
static void add(Long a, int b) {
System.out.println("Call 3 :" + (a + b));
}
static void add(int a, long b) {
System.out.println("Call 4 :" + (a + b));
}
public static void main(String[] args) {
add(3, 4);
add(3L, 4L);
add(3L,4);
add(3, 4L);
}
}
Output:
Call 1:7
Call 2:7
Call 3 :7
Call 4 :7
Upvotes: 3
Reputation: 41
It will call the method which have more specific to arguments parameters.
Ex.
int add(int a, int b) {
return a+b;
}
int add(long a, long b) {
return a+b;
}
if u call int a = add(3,4)
its call first method
& if u call
long a=3;
int c;
c = add(a,a);
c = add(a,1);
c = add(1,a);
c = add(3L,4L);
its call second one.
Upvotes: 2
Reputation: 82461
int add(int a, int b)
will be called, since 3
and 4
are both of type int
, so the compiler prefers the method that does not require a widening cast. Since a method with parameter types matching the parametes exists, it is chosen.
If at least one parameter is of type long
,
int add(long a, long b)
would be chosen instead, since a cast from long
to int
needs to be done explicitly.
add(3, 4L); // calls add(long, long)
add(3, (int) 4L); // calls add(int, int) (explicit cast long -> int)
In general you could select the argument type that uses the most bits in this case and select the method based on this information:
If it's long
, then add(long, long)
will be called. Otherwise add(int, int)
is used.
The following calls use add(int, int)
add('a', 'b'); // most bits: char (assignable to int)
add((short)1, (byte)2); // most bits: short (assignable to int)
the following calls use add(long, long)
add('a', (long)'b'); // most bits: long (not assignable to int)
add((long)1, (byte)2); // most bits: long (not assignable to int)
Upvotes: 6
Reputation: 131346
If you call add(3,4)
int add(int a, int b)
will be called because 1
and 3
are int by default (it is in the specification).
So if you want another number type, the relevant suffix should be added in the declaration of the value. For example 1L
and 3L
use long types.
So if you call add(3L,4L)
, int add(long a, long b)
will be called.
Integer Literals
An integer literal is of type long if it ends with the letter L or l; otherwise it is of type int. It is recommended that you use the upper case letter L because the lower case letter l is hard to distinguish from the digit 1.
More details in Oracle documentation on data types here
Upvotes: 3