Results 1 to 15 of 15
Like Tree1Likes
  • 1 Post By jim829

Thread: Vararg Overloading

  1. #1
    boundless is offline Member
    Join Date
    Jan 2013
    Posts
    5
    Rep Power
    0

    Question Vararg Overloading

    public class Vararg{


    static int Add(char ...c)
    {
    return 786;
    }
    static int Add(int ...x)
    {
    int sum = 0;
    for(int y : x)
    System.out.println("-> "+ y);
    System.out.println("VARARG");

    return sum;
    }
    public static void main(String ...l)
    {
    System.out.println(Add());
    }

    }
    *****************
    when i compile this code i got an output - "786", i mean both the "Add()" functions are specific for the calling statement, inspiteof "Add(char ...c)" executed. i m not able to understand why not "Add(int ...x)" executed. can any one make me understand plz. thanks in advance.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,528
    Rep Power
    5

    Default Re: Vararg Overloading

    I couldn't find the specific passage in the JLS but in the case of multiple possibilities, the most specific method is chosen. Since a long is a super set of integer values and an integer value is a super set of character values (in terms of range), I am assuming that the char vararg method is chosen because it would be the more specific. If you remove it, then the int would be chosen in preference to long.

    All this is an assumption. Those in the know may be able to cite a more accurate explanation.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    boundless is offline Member
    Join Date
    Jan 2013
    Posts
    5
    Rep Power
    0

    Default Re: Vararg Overloading

    thanks, my intuition was same but I wanted to be confirmed.

  4. #4
    Kagiso is offline Member
    Join Date
    Nov 2012
    Location
    Johannesburg, South Africa
    Posts
    92
    Rep Power
    0

    Default Re: Vararg Overloading

    Are you sure that this code was successfully compiled? in your main method you called a no argument add() method which doesn't exist
    And if you had called the method like this
    add(1) it would choose add(int... x) because values with no decimals are implicitly ints
    for you to actualy call add(char... k) you would have to pass a char value, like char k = 'k';

    If you only had one method which takes ints, add(int... x) and you passed a char value it would still compile fine because of WIDENING
    a char is implicitly an int

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Vararg Overloading

    Varargs don't require you to include any parameters for the vararg, so yes this compiles and executes.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    Kagiso is offline Member
    Join Date
    Nov 2012
    Location
    Johannesburg, South Africa
    Posts
    92
    Rep Power
    0

    Default Re: Vararg Overloading

    OOPS! I've learnt another Lesson for the day..... Thanx Tolls

    Note:
    I've tried to compile it just now... it gave an error!!!
    it reads: refference to Add is ambiguous both method Add(char...) and Add(int...) in Vararg match System.out.println(Add());

    So the compiler won't know which method to take... Please check it

    And THANX again for alerting me about var-args

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,528
    Rep Power
    5

    Default Re: Vararg Overloading

    I checked it. It works. Please show your code.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  8. #8
    Kagiso is offline Member
    Join Date
    Nov 2012
    Location
    Johannesburg, South Africa
    Posts
    92
    Rep Power
    0

    Default Re: Vararg Overloading

    That's suprising... I copied the code from this thread and compiled it... it doesn't compile
    And I never changed a thing... I just took it the way it is

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Vararg Overloading

    Also what version of the JDK?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  10. #10
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,528
    Rep Power
    5

    Default Re: Vararg Overloading

    Here is my version. Works in 1.7 and 1.6.

    Java Code:
    public class VarargTest {
    
       public static void main(String[] args) {
          System.out.println(add());
       }
       public static int add(char... a) {
          System.out.print("char returning ");
          return 1;
       }
       public static int add(int... a) {
          System.out.print("int returning ");
          return 2;
       }
       public static int add(long... a) {
          System.out.print("long returning ");
          return 3;
       }
    }
    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  11. #11
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Vararg Overloading

    I would have thought add() in the main() method of #10 was ambiguous.

  12. #12
    Kagiso is offline Member
    Join Date
    Nov 2012
    Location
    Johannesburg, South Africa
    Posts
    92
    Rep Power
    0

    Default Re: Vararg Overloading

    Quote Originally Posted by pbrockway2 View Post
    I would have thought add() in the main() method of #10 was ambiguous.
    Thanks pbrockway2

    @ Jim...I guess I can't argue with you because I use 1.5

    But what about the infamous "Java's Backward Compatibility"?

    I'll have a look at 1.7's api docs try and learn something about those changes
    Last edited by Kagiso; 05-21-2013 at 11:26 PM.

  13. #13
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,528
    Rep Power
    5

    Default Re: Vararg Overloading

    Java is backward compatible. All apps written with older versions will work with future versions of Java (excluding the occasional bug fixes that folks take advantage of but are not part of the JLS). Think about it. You can use 1.5 to compile a 1.4 compatible source. But the reverse is not necessarily true (e.g. generics).

    Regards,
    Jim
    Last edited by jim829; 05-22-2013 at 12:22 AM.
    Kagiso likes this.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  14. #14
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Vararg Overloading

    Quote Originally Posted by pbrockway2 View Post
    I would have thought add() in the main() method of #10 was ambiguous.
    As jim says, it seems the interpretation seems to be that the most specific method is chosen, that is the one with the type that can't accept types from the other methods.
    You can see that happening if you add a version of the method that takes a byte. The compiler then has nothing to choose between char and byte.

    Not sure when this was changed, but it could be something from Oracle since they seem to be in a "rewrite" mode, whereas Sun tended to be in an "if it ain't broke" mode.
    Worse, Oracle has actually changed at least one thing that was defined in the spec (in this case the Swing API) as working one way in 1.6, but no longer in 1.7.

    Here's the 1.6 doc for setText in JTextComponent.

    Here's the 1.7 version.

    Spot the (rather large) difference.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  15. #15
    Kagiso is offline Member
    Join Date
    Nov 2012
    Location
    Johannesburg, South Africa
    Posts
    92
    Rep Power
    0

    Default Re: Vararg Overloading

    I've given it a thought... Now I've gained more understanding of "backward compatibility".... I was ready for war... And you've shut my mouth

    Thanks

Similar Threads

  1. Vararg's error.
    By MustSeeMelons in forum New To Java
    Replies: 2
    Last Post: 10-13-2012, 02:10 PM
  2. Overloading or Overriding...
    By chathura87 in forum New To Java
    Replies: 5
    Last Post: 02-22-2011, 01:46 AM
  3. overloading
    By Bhuvan in forum Advanced Java
    Replies: 6
    Last Post: 01-02-2011, 09:15 AM
  4. Overloading qn
    By diskhub in forum New To Java
    Replies: 18
    Last Post: 06-07-2010, 05:19 PM
  5. Overloading?
    By padutch2 in forum New To Java
    Replies: 2
    Last Post: 12-31-2007, 03:26 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •