Skip to the content
onlineexamguide
  • Home
  • Courses
  • Engg. Interview
    • Placement Papers
    • Electrical Engineering
    • Mechanical Engineering
    • Automobile Engineering
    • Civil Engineering
    • Computer Science Engineering
    • Chemical Engineering
  • Online Exam
    • NTA UGC NET Exam
    • SSC Examination quiz
    • TET Examination Quiz
    • Banking Exam
    • Aptitude Tests
    • Computer Knowledge Tests
    • Logical Reasoning Tests
    • English Language Tests
    • Staff Nurse Exams
    • General Knowledge Tests
    • Networking Tests
  • Ghatna Chakra
  • Register
    • Instructor Registration
    • Student Registration
  • User Login
  • Home
  • Courses
  • Engg. Interview
    • Placement Papers
    • Electrical Engineering
    • Mechanical Engineering
    • Automobile Engineering
    • Civil Engineering
    • Computer Science Engineering
    • Chemical Engineering
  • Online Exam
    • NTA UGC NET Exam
    • SSC Examination quiz
    • TET Examination Quiz
    • Banking Exam
    • Aptitude Tests
    • Computer Knowledge Tests
    • Logical Reasoning Tests
    • English Language Tests
    • Staff Nurse Exams
    • General Knowledge Tests
    • Networking Tests
  • Ghatna Chakra
  • Register
    • Instructor Registration
    • Student Registration
  • User Login

VarArgs in java or Variable Arguments Explained with Examples Tutorial

VarArgs in java

Study and learn Interview MCQ Questions and Answers on VarArgs in java or Variable Arguments in Java. Attend job interviews easily with these Multiple Choice Questions

Java Varargs refer to Variable Arguments. Java Varargs are applicable only for Constructors and Methods. Let us know more in Java Tutorial.

VarArgs in java or Variable Arguments Explained

Java Varargs allow writing constructors or methods that accept any unknown (arbitrary) number of arguments. The number of arguments may be zero also. These Java Variable Arguments successfully implement Method Overloading or Constructor Overloading by maintaining unique method signatures.

You can make any Variable of Primitive or Object type, a Variable Arguments (Varargs) type. To do this, you need to add Three Dots (…) next to the Data-Type within the Parameters List of a method or constructor.

A Java Vararg variable is treated as an Array. A Vararg variable can come only in the last position in a parameter list. In a single Method or Constructor, only one Varargs type variable defined.

Java Varargs in a Constructor

The syntax is as follows.

CONSTRUCTOR_NAME(TYPE... VARIABLE_NAME)
{
  //CODE for initialization
}

Check the below example that implements Varargs inside a Constructor’s Parameter List.

public class VarargsExample
{
  VarargsExample(int... a)
  {
    if(a.length == 0)
      System.out.println("Zero Arguments");
    for(int i=0; i<a.length; i++)
      System.out.print(a[i] + ",");
    System.out.println();
  }

  VarargsExample(String name, int...a)
  {
    System.out.println("Name= " + name);
    for(int i=0; i<a.length; i++)
      System.out.print(a[i] + ",");
  }

  public static void main(String[] args)
  {
    VarargsExample obj = new VarargsExample();
    VarargsExample ve = new VarargsExample(5,6,7);
    VarargsExample ve2 = new VarargsExample("CANADA", 1,2);
  }
}
//OUTPUT
Zero Arguments

5,6,7,
Name= CANADA
1,2,

Notice that we have not maintained a Zero-Argument constructor in the above example. Even then, the constructor with int…a arguments is invoked for new VarargsExample() call. If you put a constructor with zero arguments, it will be invoked instead of Variable Arguments type constructor if it is the first constructor matched for zero arguments.

Let us check another example that shows incompatible constructor overloading using Varargs.

public class VarargsExample
{
  VarargsExample(boolean...a)
  {

  }
  VarargsExample(int... a)
  {

  }
  public static void main(String[] args)
  {
    VarargsExample ve = new VarargsExample(); //Error
  }

}
//OUTPUT
Compiler error

The above example produces an error “The Constructor is Ambiguous“. Because int and boolean are incompatible or inconvertible data types. If we replace boolean with a number data type like float, double, long or byte, the program compiles and gives the output.

Note: While choosing a Vararg constructor, the compiler selects the one with a small-sized data type first. So CONSTRUCTOR(byte…a) is selected before CONSTRUCTOR(int…a).

Java Varargs in a Method

Just like in a Constructor with Varargs, Java methods too can have Varargs. Only one Vararg type variable is allowed in a method that too at the end of the parameters list.

The syntax is as follows.

METHOD_NAME(TYPE... VARIABLE_NAME)
{
  //CODE for initialization
}

Check the below example that implements Varargs inside a Method’s Parameter List.

public class VarargsMethodExample
{
  void show(String...names)
  {

  }
  void show(int...codes)
  {

  }
  void modify(String operator, long...transmission)
  {

  }
  String report(float var, int...codes)
  {
    String a = "";
    for(int i=0; i<codes.length; i++)
    {
      a = a + codes[i] + "," ;
    }
    return a;
  }

  public static void main(String[] args)
  {
    VarargsMethodExample vme = new VarargsMethodExample();
    String rep = vme.report(0.0f, 2,3,4);
    System.out.println("Report= " + rep);
  }
}
//OUTPUT
Report= 2,3,4,

In the above example, we have used Java Varargs in the methods show(), modify() and report(). Method “show” is overloaded. Method “report” even returns a value.

VarArgs online test

[WpProQuiz 163]


For More Free online Quizzes of JAVA PROGRAMMING Test online Click here


For More Free online Test of previous years Question Papers of Aptitude Click here

Write a comment Cancel reply

You must be logged in to post a comment.

Recent Posts

  • Atal Bihari Vajpayee | श्री अटल बिहारी वाजपेयी जीवन परिचय
  • सम्राट अशोक का जीवन परिचय (Emperor Ashoka)
  • Prithviraj Chauhan
  • बाल गंगाधर तिलक
  • स्वामी दयानंद सरस्वती
  • UPPSC Mines Inspector Recruitment 2022 Notification Out
  • AIIMS Delhi JR Vacancy 2022 [194 Post] Notification and Apply Online
  • भीमराव अम्बेडकर
  • डॉक्टर राजेंद्र प्रसाद का जीवन परिचय
  • श्रीनिवास रामानुजन का जीवन परिचय
  • Amnesty International day
  • World Economic Forum
  • UPSSSC VDO Syllabus and Exam Pattern 2022
  • RBI Officer Grade B Recruitment 2022
  • UKMSSB Assistant Professor Recruitment 2022 Apply Now 339 Post

onlineexamguide

onlineexamguide.com is the ultimate guide that will keep you updated about almost every Exam & Interviews . We aim to provide our readers with an informative details that have been occurring in Examination . Here at onlineexamguide.com , we focus on delivering our readers with the latest exam Pattern Mock test

We Provide Free online test to practice for Competitive exams , Online Exam, Entrance and Interview. Learn and Practice online test for Free and Prepare for your exam online with us

Quick links

  • About us
  • Privacy Policy
  • Instructor Registration
  • Student Registration
  • Java Programming Tests
  • C programming Tests
  • C++ programming Tests
  • Aptitude Tests

Follow us

Free Online Mock Test

  • UPTET PRIMARY Online Test Series
  • Super TET Mock Test in Hindi 2022
  • CTET Mock Test 2022 Paper 1
  • SSC CHSL Online Mock Test
  • SSC MTS Mock Test 2022
  • SSC CGL Mock Test
  • SSC GD Mock Test
  • ccc online test

Search

Learn and Earn

Register as Instructor - Create and sell online courses and coaching services with the best online platform onlineexamguide.com . Build a course, build a brand, earn money

Contact us

For any queries

Email us on - admin@onlineexamguide.com

We will response fast as much as we can
Copyright © 2022 onlineexamguide.com - All Rights Reserved.
error: Content is protected !!

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.