Java With Us
Home   |   Tutorial   |   Programs  

Java Tutorial

Introduction to Java
Hello World Program
Variables and Data types
More about data types
Displaying text using print and println
Displaying text using printf
Java Comments
Naming conventions for Identifiers
Mathematical operations in Java
Taking input from the user

Classes and Objects
Introduction to object oriented programming
The constituents of a Class
Creating objects and calling methods
Get and Set Methods
Default constructor provided by the compiler
Access Specfiers
Scope and lifetime of Variables
Call by value and Call by Reference

A few more topics
Casting
Class as a reference data type
Constants or literals
Final variables
Increment and decrement operators
Manipulating Strings
Operators
Overloading constructors and methods
Static methods and variables
The Java API
The Math class
this keyword
Wrapper classes

Control Structures
Control Statements
Repetition statements
Nested loops
Formulating algorithms
Branching Statements

Arrays
Arrays introduction
Processing arrays using 1oops
Searching and sorting arrays
Array of objects
Multi dimensional arrays
Taking input as command line arguments
Using ellipsis to accept variable number of arguments

Inheritance
Inheritance introduction
Relation between a super class and sub class
Final classes and methods
The protected access specifier
Class Object

Polymorphism
Introduction
Interfaces
Packages
Abstract classes and methods

Exception handling
Exception handling introduction
Exception hierarchy
Nested try catch blocks
Throwing exceptions


Using Ellipsis to Accept Variable Number of Arguments

A method can receive a variable number of arguments with the use of ellipsis (three consecutive dots: ...) In order to do so, the data type of the variable number of arguments which can be even a reference type is stated followed by the ellipsis and an identifier. This type can be used not more than once in a method header and should be the last of all parameters. Following lines show a few valid and invalid method declarations that use variable length argument lists.

public void meth ( int... a) // valid
public void meth (double a, int... b) // valid
public void meth ( int... a, int b) // invalid- Ellipsis may be used towards the end only
public void meth ( int... a, double... b) // invalid - More than one variable length parameter list may not be used
public void meth ( Student... a) // valid - Reference types are also allowed
public void meth( int[]... a) // valid - reference types are also allowed

The arguments received are stored in an array of the same data type as specified in the method header having a length equal to the number of arguments passed. Therefore, those elements can be accessed as we access an array passed to an array type parameter. Here is an example of a class Add containing a method addVariable() which takes variable number of arguments, add them and displays the result. This method is invoked through the main method with 0, 1 and 2 arguments.

public class Add {

    public static void main(String[] args) {
        addVariable();
        addVariable(1);
        addVariable(3, 4);
    }

    public static void addVariable(int... a) {
        int sum = 0;
        for (int i = 0; i < a.length; i++) {
            sum = sum + a[i];
        }
        System.out.println(a.length + " arguments received whose sum is " + sum);
    }
}

Here is the output of this program.

0 arguments received whose sum is 0
1 arguments received whose sum is 1
2 arguments received whose sum is 7

The method taking variable arguments is called only when no exact match of arguments occur for a given method call. For example if we had a method that accepts a single int argument in the above class, the method call addVariable(1) would invoke this particular method rather than the one taking variable number of arguments. Verify this fact yourself by modifying the code above.

Privacy Policy