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 3 of 3

Thread: Variable length arguments and Ambiguity

  1. #1
    Junior Member
    Join Date
    Sep 2018
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Variable length arguments and Ambiguity

    In this code I have taken an overloaded method named show() with vararg. One is having int type vararg and the other is having float type vararg. When the other vararg is float then no error is there. But when it is replaced by boolean the system is giving an error. The code for both is given below.
    //code with float vararg
    class A
    {
    	void show(int ...x)
    	{
    		System.out.println("show with int");
    	}
    	void show(float ...x)
    	{
    		System.out.println("show with float");
    	}
    }
    class B_float
    {
    	public static void main(String a[])
    	{
    		A ob=new A();
    		ob.show();
    	}
    }
     
    /code
     
    //code with boolean type vararg
    class A
    {
    	void show(int ...x)
    	{
    		System.out.println("show with int");
    	}
    	void show(boolean ...x)
    	{
    		System.out.println("show with float");
    	}
    }
    class B_bool
    {
    	public static void main(String a[])
    	{
    		A ob=new A();
    		ob.show();
    	}
    }
    Last edited by Norm; September 3rd, 2018 at 06:19 AM. Reason: added code tags

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Variable length arguments and Ambiguity

    the system is giving an error.
    Please copy the full text of the error message and paste it here. It has important info about the error.

    To wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member tonya's Avatar
    Join Date
    Feb 2018
    Posts
    16
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Variable length arguments and Ambiguity

    Your overloaded method has ambiguous calls in class B_float and class B_bool. For the bool call to work you need to pass a bool in (as per the below)

    class B_bool
    {
    	public static void main(String a[])
    	{
    		A ob=new A();
    		ob.show(true);
    	}
    }

Similar Threads

  1. Replies: 18
    Last Post: March 30th, 2013, 09:11 AM
  2. What is the difference between instant variable and instance variable?
    By avistein in forum Java Theory & Questions
    Replies: 7
    Last Post: January 6th, 2013, 11:42 PM
  3. Replies: 0
    Last Post: October 30th, 2012, 10:06 AM
  4. For-loop: initialisation of variable, can't set variable to value zero?
    By Bitbot in forum Loops & Control Statements
    Replies: 4
    Last Post: July 15th, 2012, 02:32 PM
  5. Replies: 5
    Last Post: November 16th, 2011, 11:22 AM