Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Why does this code compile?

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Why does this code compile?

    Hello,

    Can someone please tell me why this code compiles? I thought it must give an error because of an ambiguous call to varargs(), but it prints "Hello1" when varargs(1 ) is called.
    package test;
     
    public class Test {  
     
            public static void varargs(int x, int ...v) {  
              System.out.println("Hello2");
        }  
        public static void varargs(int x) {  
              System.out.println("Hello1");
        }  
        /**
         *
         * @param x
         * @param y
         */
     
     
        public static void main(String[] args) {  
              System.out.println("Main method");
              varargs(1);
        }  
    }
    Last edited by helloworld922; December 28th, 2012 at 10:41 PM. Reason: please use [code] tags


  2. #2
    Member Staticity's Avatar
    Join Date
    Jul 2011
    Location
    Texas
    Posts
    105
    My Mood
    Inspired
    Thanks
    3
    Thanked 5 Times in 5 Posts

    Default Re: Why does this code compile?

    The program compiles and will print out "Hello1" since you called the method, varargs, that accepts a single int as a parameter. Everything seems to be fine.
    Simplicity calls for Complexity. Think about it.

  3. #3
    Junior Member
    Join Date
    Dec 2012
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Why does this code compile?

    How does the program distinguish which method to call? Since both methods take 1 int, I could be calling either of them (because varargs can take no arguments also).

  4. #4
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    1,069
    Thanks
    7
    Thanked 215 Times in 191 Posts

    Default Re: Why does this code compile?

    The place to go for an answer to questions like this is the Java Language Specification. In this case the relevant part is 15.12 Method Invocation Expressions (chapter 15 deals with the meaning of all the different sorts of expression that can arise in Java).

    Be warned, the specification aims at being comprehensive and definitive not at being readily comprehensible! When it comes to this particular question, 15.12 begins with the observation that "Resolving a method name at compile time is more complicated than resolving a field name because of the possibility of method overloading. Invoking a method at run-time is also more complicated than accessing a field because of the possibility of instance method overriding". So for someone starting out the thing to understand is that the non varargs form will be preferred if there is a choice: that's just how Java is. So there is no ambiguity. And no problem with compilation.

    ---

    The messy details are spelt out in 15.12, which shows how varargs takes its place alongside all the other things that have to be determined as the compiler attempts to compile both forms of the varargs() method, or as the runtime evaluates expressions involving that method. In particular the first step of 15.12.2 Compile-Time Step 2: Determine Method Signature says "The first phase (15.12.2.2) performs overload resolution without permitting boxing or unboxing conversion, or the use of variable arity method invocation. If no applicable method is found during this phase then processing continues to the second phase".

    In other words variable arity methods are only considered for an expression like "varargs()" is there is no match with a no argument form. There is a note following this statement giving a rationale in terms of being compatible with old code (calling the method) from before Java 5, if the varargs form is added later.

  5. The Following 2 Users Say Thank You to pbrockway2 For This Useful Post:

    Jire (January 1st, 2013), prasanna1157 (December 29th, 2012)

  6. #5
    Junior Member
    Join Date
    Dec 2012
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Why does this code compile?

    Thank you.

  7. #6
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    1,069
    Thanks
    7
    Thanked 215 Times in 191 Posts

    Default Re: Why does this code compile?

    You're welcome.

Similar Threads

  1. Replies: 1
    Last Post: November 2nd, 2012, 05:16 PM
  2. Code won't compile
    By JavaChallenged in forum What's Wrong With My Code?
    Replies: 6
    Last Post: February 12th, 2012, 08:18 PM
  3. Code won't compile, but can't find the error....
    By RockDoc in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 29th, 2012, 02:09 AM
  4. Cannot get to compile
    By Goff256 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 19th, 2011, 02:09 PM
  5. [SOLVED] Picture won't go with the .jar (Wrong code or compile error?)
    By Fermen in forum Object Oriented Programming
    Replies: 3
    Last Post: March 15th, 2011, 06:22 PM