Java Homework Help | Help with Java Programming | Programming Homework Help | Java Programming Assignment Help | Help with Java Programming Assignment | Java Tutor | Programming Help | Java Online Help | Java Programming Online | Java Programming Projects

Varargs is the method by which we can use variable number of arguments in one method in java programming.Varargs: Methods with variable number of arguments. Its very useful for students in programming help. To solve real time problems in java programming varargs is important. Following is defination and example of varargs :-
In java we can create methods that takes variable number of arguments. This feature is called varargs or variable number of arguments. The method which accepts variable number of arguments is called varargs method.
Syntax
returnType methodName(dataType … parameterName){

}

"..." tells the compiler that this is a vararg.

Example
class VarargsEx{
public void printData(int ... a){
System.out.println("Parameters are " + a.length);
for(int i=0; i < a.length; i++)
System.out.print(a[i] + " ");
System.out.println();
}

public static void main(String args[]){
VarargsEx obj = new VarargsEx();
obj.printData(1);
obj.printData(1,2);
obj.printData(1,2,3);
}
}

Output
Parameters are 1
1
Parameters are 2
1 2
Parameters are 3
1 2 3

The syntax for the method printData, tells the compiler, that this method can accept zero or more arguments. So all the calls like below are valid.
obj.printData(1);
obj.printData(1,2);
obj.printData(1,2,3);

The variable number of parameters (varargs) must be at last while defining a method with varargs.

There must be only one vararg parameter in method definition.

Some Points to Remember
1. The variable number of parameters (varargs) must be at last while defining a method with varargs.
Example
class VarargsEx{
public void printData(int ... a, float f1){
System.out.println("Parameters are " + a.length);
for(int i=0; i < a.length; i++)
System.out.print(a[i] + " ");
System.out.println();
}
}

When you try to compile the above program, compiler will generates error like below

VarargsEx.java:3: error: ')' expected
public void printData(int ... a, float f1){
^
VarargsEx.java:3: error: ';' expected
public void printData(int ... a, float f1){
^
2 errors

valid method definition is
public void printData(float f1, int ... a)

2. There must be only one vararg parameter in method definition.
Example
class VarargsEx{
public void printData(int ... a1, int ... a2){
}
}

When you try to compile the above code, compiler throws error like below
VarargsEx.java:3: error: ')' expected
public void printData(int ... a1, int ... a2){
^

VarargsEx.java:3: error: <identifier> expected
public void printData(int ... a1, int ... a2){
^

VarargsEx.java:3: error: <identifier> expected
public void printData(int ... a1, int ... a2){
^
3 errors

 

Overloading Methods
Methods within a class can have the same name if they have different parameter lists.

For Example
int sum(int a, int b);
int sum(int a, int b, int c);

Both methods have the same name sum, and parameters are different.

Overloading methods is differentiated by the number and type of arguments passed to the method.

The return type is not sufficient to differentiate two overload methods

Example
class OverloadEx{
void print(String s){
System.out.println("I am String " + s);
}

void print(int a){
System.out.println("I am integer " + a);
}

void print(float f){
System.out.println("I am float " + f);
}

public static void main(String args[]){
OverloadEx obj = new OverloadEx();
obj.print("HI");
obj.print(9);
obj.print(10.01f);
}
}

Output
I am String HI
I am integer 9
I am float 10.01

Class OverloadEx, overloaded the method print.

Some points to remember
1. The return type is not sufficient to differentiate two overloaded methods.
When you try to differentiate two methods with the return type, then you will get compile time error like “Method is already defined”.
class OverloadEx{
String print(int s){
System.out.println("I am integer " + s);
}

void print(int a){
System.out.println("I am integer " + a);
}
}

When you try to compile you will get the below error
OverloadEx.java:7: error: method print(int) is already defined in class OverloadEx
void print(int a){
^
1 error

2. By default a decimal number considered to be an integer
class OverloadEx{
void print(int s){
System.out.println("I am integer " + s);
}

void print(byte a){
System.out.println("I am byte " + a);
}

void print(short a){
System.out.println("I am short " + a);
}

void print(long a){
System.out.println("I am long " + a);
}

public static void main(String args[]){
OverloadEx obj = new OverloadEx();
obj.print(10);
}
}

Output
I am integer 10

Above program overloads method print, with 4 parameters byte, short, int and long. Program calls the print() with value 10. By default numbers consider to be primitive. So print with int argument will be called.

3. By default a real value considers as double
class OverloadEx{
void print(float s){
System.out.println("I am float " + s);
}

void print(double a){
System.out.println("I am double " + a);
}

public static void main(String args[]){
OverloadEx obj = new OverloadEx();
obj.print(10.01);
}
}

Output
I am double 10.01

4. What is the output of the below program ?

class OverloadEx{
void print(float s){
System.out.println("I am float " + s);
}

public static void main(String args[]){
OverloadEx obj = new OverloadEx();
obj.print(10.01);
}
}

A real number considers as double by default. So program gives compile time error like below
OverloadEx.java:9: error: method print in class OverloadEx cannot be applied to given types;
obj.print(10.01);
^
required: float
found: double
reason: actual argument double cannot be converted to float by method invocation conversion
1 error

5. What is the output of the below program ?

class OverloadEx{
void print(float s){
System.out.println("I am float " + s);
}

public static void main(String args[]){
OverloadEx obj = new OverloadEx();
obj.print(10.01f);
}
}

Output
I am float 10.01

Our Team

We have 2500+ expert pool from all over the world. They are highly qualified and well experienced. All experts have experinced in teaching and good command over programming topics. Some of our experts are as follows:


John Doe

JAVA EXPERT(10+ Yrs Exp.)

Larry Doe

C EXPERT(12+ Yrs Exp.)

Ranith Kays

C++ EXPERT(8+ Yrs Exp.)

Joan Ray

OPEN SOURCE LANGUAGE EXPERT(8+ Yrs Exp.)