0% found this document useful (0 votes)
2 views13 pages

Lecture 15-A Varargs

The document discusses command-line arguments and variable-length arguments (varargs) in Java. It explains how command-line arguments allow users to input data through the console and how varargs simplify method creation for variable numbers of arguments. Additionally, it covers the syntax, usage, and limitations of varargs, including examples and the importance of method overloading.

Uploaded by

Amaan Sadri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views13 pages

Lecture 15-A Varargs

The document discusses command-line arguments and variable-length arguments (varargs) in Java. It explains how command-line arguments allow users to input data through the console and how varargs simplify method creation for variable numbers of arguments. Additionally, it covers the syntax, usage, and limitations of varargs, including examples and the importance of method overloading.

Uploaded by

Amaan Sadri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

OBJECT ORIENTED PROGRAMMING

(CS F213)
COMMAND LINE AND VARIABLE
LENGTH ARGUMENTS

Lecture 15-A
Prof. Anita Agrawal,
BITS, Pilani-K.K.Birla Goa Campus
2/17/2023 11:12:33 AM Anita Agrawal CS F213 Object Oriented Programming 2

Command-Line Arguments
• Command line arguments is a methodology in which user can
give inputs through the console.

• A Java application can accept any number of arguments from


the command line.

• Syntax: java class_name command_line_arguments

• JVM wraps the command-line arguments and supplies to args [ ].


• can be checked by using args.length
2/17/2023 11:12:33 AM Anita Agrawal CS F213 Object Oriented Programming 3

Variable Arguments (var…args)


• Varargs…(Variable Arguments): A feature in Java that
simplifies the creation of methods that need to take a
variable number of arguments.

• A method that takes a variable number of arguments is a


varargs method.

• An argument that can accept variable number of values is a


vararg.

• Syntax:
(…) A variable-length argument is specified by three periods
2/17/2023 11:12:33 AM Anita Agrawal CS F213 Object Oriented Programming 4

Example:

public static void fun (int ... a)


{
// method body
}
2/17/2023 11:12:33 AM Anita Agrawal CS F213 Object Oriented Programming 5

How varargs work behind the scene…


• The ... syntax tells the Java compiler that the method
can be called with zero or more arguments.
void f1(int …x)

• As a result, x variable is implicitly declared as an


array of type int[ ]. Thus, inside the method, x variable
is accessed using the array syntax.

• In case of no args, the length of x is 0.


2/17/2023 11:12:33 AM Anita Agrawal CS F213 Object Oriented Programming 6

Other parameters
• The varargs method can have other parameters too in
addition to varargs.
• But there can exist only one varargs parameter.
• The varargs parameter should be written last in the
parameter list of method declaration.
Example:
• int sum (int x, float y, double … z)
- The first two arguments are matched with the first two
parameters and the remaining arguments belong to z
2/17/2023 11:12:34 AM Anita Agrawal CS F213 Object Oriented Programming 7

Erroneous varargs examples


• Specifying varargs as the middle parameter of
method instead of last one:
int sum (int x, double … z, float y )

• Specifying two varargs in a single method:


void method(string...abc, int... x)

• Specifying varargs as the first parameter of method


instead of last one:
void method(int...x, String abc)
2/17/2023 11:12:34 AM Anita Agrawal CS F213 Object Oriented Programming 8

Example: Method using NoVarags


class NoVararg {

public int sum(int a, int b){


return a+b;
}

public int sum(int a, int b, int c){


return a+b+c;
Output:
} 3
} 6
Class A{
public static void main( String[] args ) {
NoVararg x = new NoVararg();
System.out.println(x.sum(1, 2));
System.out.println(x.sum(1, 2, 3));
}
}
2/17/2023 11:12:34 AM Anita Agrawal CS F213 Object Oriented Programming 9

• sum() method had to be overloaded to make it


work for 3 arguments.

• What if the user wants to add 5 numbers or 10


or 100?
…......This can be handled in a neat way with the
use of varargs.
2/17/2023 11:12:34 AM Anita Agrawal CS F213 Object Oriented Programming 10

Example Method using varargs


…Vararg.java
2/17/2023 11:12:34 AM Anita Agrawal CS F213 Object Oriented Programming 11

• As you can see, varargs can be really useful in some


situations. However, if you are certain about the number
of arguments passed to a method, use method
overloading instead. For example, if you are certain that
sum() method will be used only to calculate the sum of
either 2 or 3 arguments, use overloading like in the first
example.
2/17/2023 11:12:34 AM Anita Agrawal CS F213 Object Oriented Programming 12

Varargs method overloading

• Similar to typical methods, we can overload


vararg methods too.
2/17/2023 11:12:34 AM Anita Agrawal CS F213 Object Oriented Programming 13

class VarargOverload {
public void A(int ... args){
int sum = 0;
for (int i: args) {
sum += i;
}
System.out.println("sum = " + sum);
}
public void A(String ... args){
System.out.println("args.length = "+ args.length);
}
}
Class B {
public static void main( String[] args ) {
VarargOverload x = new VarargOverload();
x.A(1, 2, 3);
x.A("hello", "world");
}
}

You might also like