In Java programming sum(5,6) will call for which of these functions in a class sum(double a, int b) or sum(int a, int b) ?
In Java programming sum(5,6) will call for which of these functions in a class sum(double a, int b) or sum(int a, int b) ?
Read less
In Java, the method that will be called when you write sum(5, 6) depends on method overloading resolution, which considers the most specific match based on the types of the arguments. Given: sum(5, 6); Here, both arguments are integers (int literals). And you have two overloaded methods: sum(int a,Read more
In Java, the method that will be called when you write sum(5, 6) depends on method overloading resolution, which considers the most specific match based on the types of the arguments.
Given:
sum(5, 6);
Here, both arguments are integers (int literals).
And you have two overloaded methods:
sum(int a, int b)
sum(double a, int b)
Resolution:
Java will choose the most specific method that matches the argument types without needing conversion.
sum(int a, int b) matches exactly.
sum(double a, int b) would require widening the first int to a double.
Therefore, sum(int a, int b) will be called.
Summary:
In Java, when overloading methods:
Java prefers exact matches.
Widening conversions (like int to double) are only used if no exact match is found.
So:
sum(5, 6); // calls sum(int a, int b)
See less