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

  • Home
  • SEARCH
  • 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 863737
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T09:21:41+00:00 2026-05-15T09:21:41+00:00

Is this valid Java? import java.util.Arrays; import java.util.List; class TestWillThatCompile { public static String

  • 0

Is this valid Java?

import java.util.Arrays;
import java.util.List;

class TestWillThatCompile {

    public static String f(List<String> list) {
        System.out.println("strings");
        return null;
    }

    public static Integer f(List<Integer> list) {
        System.out.println("numbers");
        return null;
    }

    public static void main(String[] args) {
        f(Arrays.asList("asdf"));
        f(Arrays.asList(123));
    }

}
  • Eclipse 3.5 says yes
  • Eclipse 3.6 says no
  • Intellij 9 says yes
  • Sun javac 1.6.0_20 says yes
  • GCJ 4.4.3 says yes
  • GWT compiler says yes
  • Crowd at my previous Stackoverflow question says no

My java theory understanding says no!

It would be interesting to know what the JLS is saying about it.

  • 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-05-15T09:21:42+00:00Added an answer on May 15, 2026 at 9:21 am

    It depends upon how you wish to call these methods. If you wish to call these methods from other Java source code, then it is considered invalid for reasons illustrated in Edwin’s answer. This is a limitation of the Java Language.

    However, not all classes need to be generated from Java source code (consider all the languages that use the JVM as their runtime: JRuby, Jython, etc…). At the bytecode level, the JVM can disambiguate the two methods because the bytecode instructions specify the return type they are expecting. For example, here is a class written in Jasmin that can call either of these methods:

    .class public CallAmbiguousMethod
    .super java/lang/Object
    
    .method public static main([Ljava/lang/String;)V
      .limit stack 3
      .limit locals 1
    
      ; Call the method that returns String
      aconst_null
      invokestatic   TestWillThatCompile/f(Ljava/util/List;)Ljava/lang/String;
    
      ; Call the method that returns Integer
      aconst_null
      invokestatic   TestWillThatCompile/f(Ljava/util/List;)Ljava/lang/Integer;
    
      return
    
    .end method
    

    I compile it to a class file using the following command:

    java -jar jasmin.jar CallAmbiguousMethod.j
    

    And call it using:

    java CallAmbiguousMethod
    

    Behold, the output is:

    > java CallAmbiguousMethod
    strings
    numbers
    

    Update

    Simon posted an example program that calls these methods:

    import java.util.Arrays;
    import java.util.List;
    
    class RealyCompilesAndRunsFine {
    
        public static String f(List<String> list) {
            return list.get(0);
        }
    
        public static Integer f(List<Integer> list) {
            return list.get(0);
        }
    
        public static void main(String[] args) {
            final String string = f(Arrays.asList("asdf"));
            final Integer integer = f(Arrays.asList(123));
            System.out.println(string);
            System.out.println(integer);
        }
    
    }
    

    Here is the Java bytecode generated:

    >javap -c RealyCompilesAndRunsFine
    Compiled from "RealyCompilesAndRunsFine.java"
    class RealyCompilesAndRunsFine extends java.lang.Object{
    RealyCompilesAndRunsFine();
      Code:
       0:   aload_0
       1:   invokespecial   #1; //Method java/lang/Object."":()V
       4:   return
    
    public static java.lang.String f(java.util.List);
      Code:
       0:   aload_0
       1:   iconst_0
       2:   invokeinterface #2,  2; //InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
       7:   checkcast       #3; //class java/lang/String
       10:  areturn
    
    public static java.lang.Integer f(java.util.List);
      Code:
       0:   aload_0
       1:   iconst_0
       2:   invokeinterface #2,  2; //InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
       7:   checkcast       #4; //class java/lang/Integer
       10:  areturn
    
    public static void main(java.lang.String[]);
      Code:
       0:   iconst_1
       1:   anewarray       #3; //class java/lang/String
       4:   dup
       5:   iconst_0
       6:   ldc     #5; //String asdf
       8:   aastore
       9:   invokestatic    #6; //Method java/util/Arrays.asList:([Ljava/lang/Object;)Ljava/util/List;
       12:  invokestatic    #7; //Method f:(Ljava/util/List;)Ljava/lang/String;
       15:  astore_1
       16:  iconst_1
       17:  anewarray       #4; //class java/lang/Integer
       20:  dup
       21:  iconst_0
       22:  bipush  123
       24:  invokestatic    #8; //Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
       27:  aastore
       28:  invokestatic    #6; //Method java/util/Arrays.asList:([Ljava/lang/Object;)Ljava/util/List;
       31:  invokestatic    #9; //Method f:(Ljava/util/List;)Ljava/lang/Integer;
       34:  astore_2
       35:  getstatic       #10; //Field java/lang/System.out:Ljava/io/PrintStream;
       38:  aload_1
       39:  invokevirtual   #11; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
       42:  getstatic       #10; //Field java/lang/System.out:Ljava/io/PrintStream;
       45:  aload_2
       46:  invokevirtual   #12; //Method java/io/PrintStream.println:(Ljava/lang/Object;)V
       49:  return
    

    It turns out the Sun compiler is generating the bytecode necessary to disambiguate the methods (see instructions 12 and 31 in the last method).

    Update #2

    The Java Language Specification suggests that this may, in fact, be valid Java source code. On page 449 (§15.12 Method Invocation Expressions) we see this:

    It is possible that no method is the most specific, because there are two or
    more methods that are maximally specific. In this case:

    • If all the maximally specific methods have override-equivalent (§8.4.2) signatures,
      then:

      • If exactly one of the maximally specific methods is not declared abstract,
        it is the most specific method.
      • Otherwise, if all the maximally specific methods are declared abstract,
        and the signatures of all of the maximally specific methods have the same
        erasure (§4.6), then the most specific method is chosen arbitrarily among
        the subset of the maximally specific methods that have the most specific
        return type
        . However, the most specific method is considered to throw a
        checked exception if and only if that exception or its erasure is declared in
        the throws clauses of each of the maximally specific methods.
    • Otherwise, we say that the method invocation is ambiguous, and a compiletime
      error occurs.

    Unless I am mistaken, this behavior should only apply to methods declared as abstract, though…

    Update #3

    Thanks to ILMTitan’s comment:

    @Adam Paynter: Your bolded text does
    not matter, because it is only a case
    when two methods are
    override-equivalent, which Dan showed
    was not the case. Thus, the
    determining factor must be if the JLS
    takes generic types into account when
    determining most specific method. –
    ILMTitan

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 430k
  • Answers 430k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Take a look at GDAL. Specifically the code used here.… May 15, 2026 at 1:54 pm
  • Editorial Team
    Editorial Team added an answer Usage of $_GET does not seem to be a good… May 15, 2026 at 1:54 pm
  • Editorial Team
    Editorial Team added an answer No, you cannot do this in MySQL. A MySQL procedure… May 15, 2026 at 1:54 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.