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) ?
Share
Sign up to our innovative Q&A platform to pose your queries, share your wisdom, and engage with a community of inquisitive minds.
Log in to our dynamic platform to ask insightful questions, provide valuable answers, and connect with a vibrant community of curious minds.
Forgot your password? No worries, we're here to help! Simply enter your email address, and we'll send you a link. Click the link, and you'll receive another email with a temporary password. Use that password to log in and set up your new one!
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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