Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7956757
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T04:05:19+00:00 2026-06-04T04:05:19+00:00

I’m trying to understand how java deals with ambiguities in function calls. In the

  • 0

I’m trying to understand how java deals with ambiguities in function calls. In the following code, the call to method is ambiguous, but method2 is not!!!.

I feel both are ambiguous, but why does this compile when I comment out the call to method? Why is method2 not ambiguous as well?

public class A {
    public static <K> List<K> method(final K arg, final Object... otherArgs) {
        System.out.println("I'm in one");
        return new ArrayList<K>();
    }

    public static <K> List<K> method(final Object... otherArgs) {
        System.out.println("I'm in two");
        return new ArrayList<K>();
    }

    public static <K, V> Map<K, V> method2(final K k0, final V v0, final Object... keysAndValues) {
        System.out.println("I'm in one");
        return new HashMap<K,V> ();
    }

    public static <K, V> Map<K, V> method2(final Object... keysAndValues) {
        System.out.println("I'm in two");
        return new HashMap<K,V>();
    }

    public static void main(String[] args) {
        Map<String, Integer> c = A.method2( "ACD", new Integer(4), "DFAD" );
        //List<Integer> d = A.method(1, "2", 3  );
    }
}

EDIT:
This came up in comments: A number of IDEs report both as ambiguous – IntelliJ and Netbeans so far. However, it compiles just fine from command-line/maven.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T04:05:21+00:00Added an answer on June 4, 2026 at 4:05 am

    An intuitive way to test whether method1 is more specific than method2 is to see whether method1 can be implemented by invoking method2 with the same parameters

    method1(params1){
        method2(params1);   // if compiles, method1 is more specific than method2
    }
    

    If there are varargs, we may need to expand a vararg so that 2 methods have same number of params.

    Let’s check the first two method()s in your example

    <K> void method_a(K arg, Object... otherArgs) {
        method_b(arg, otherArgs);   //ok L1
    }
    <K> void method_b(Object arg, Object... otherArgs) { // extract 1 arg from vararg
        method_a(arg, otherArgs);   //ok L2
    }
    

    (return types are not used in determining specificity, so they are omitted)

    Both compile, therefore each is more specific than the other, hence the ambiguity. Same goes for your method2()s, they are more specific than each other. Therefore the call to method2() is ambiguous and shouldn’t compile; otherwise it’s a compiler bug.


    So that’s what the spec says; but is it proper? Certainly, method_a looks more specific than method_b. Actually if we have a concrete type instead of K

    void method_a(Integer arg, Object... otherArgs) {
        method_b(arg, otherArgs);   // ok
    }
    void method_b(Object arg, Object... otherArgs) {
        method_a(arg, otherArgs);   // error
    }
    

    then only method_a is more specific than method_b, not vice versa.

    The discrepancy arises from the magic of type inference. L1/L2 calls a generic method without explicit type arguments, so the compiler tries to infer the type arguments. The goal of type inference algorithm is to find type arguments such that the code compiles! No wonder L1 and L2 compile. L2 is actually infer to be this.<Object>method_a(arg, otherArgs)

    Type inference tries to guess what the programmer wants, but the guess must be wrong sometimes. Our real intention is actually

    <K> void method_a(K arg, Object... otherArgs) {
        this.<K>method_b(arg, otherArgs);   // ok
    }
    <K> void method_b(Object arg, Object... otherArgs) {
        this.<K>method_a(arg, otherArgs);   // error
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I want to construct a data frame in an Rcpp function, but when I
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I am trying to render a haml file in a javascript response like so:
I have this code to decode numeric html entities to the UTF8 equivalent character.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.