Java Tutorials
  • Introduction to Java
    • What is Java?
    • History and Features of Java
    • Java Virtual Machine (JVM) and Bytecode
    • Why Java?
  • Setting up Java Development Environment
    • Installing Java Development Kit (JDK)
    • JDK vs JRE
    • Setting up IDE (Eclipse, IntelliJ, NetBeans) or Text Editor (VS Code, Sublime Text)
  • Basic Java
    • First Java Program : Hello World
    • Variable
    • Data Type
    • Constant
    • Date and Format
    • Operator
    • Condition
    • Looping
    • Function
    • Variadic Function
    • Enums
    • Array
    • Collection
    • Exception and Exception Handling
    • Naming Convention
  • Object Oriented Programming (OOP)
    • Classes and Objects
    • Inheritance and Polymorphism
    • Encapsulation and Abstraction
  • File Handling
    • Reading and Writing Binary File
    • Reading and Writing Text File
    • Serialization and Deserialization
  • Multithreading
    • Creating and Running Threads
    • Synchronization
    • Thread Pools and Executors
  • Collections API
    • Sorting and Comparable
    • Searching and Comparator
  • Java Database Connectivity (JDBC)
    • Introduction and Life Cycle
    • Connection to Database (MySQL)
    • Downloading JDBC Drivers for Various Databases
    • Maven and Gradle JDBC Drivers for Various Databases
    • JDBC URL Formats
    • Statement and PreparedStatement
    • CallableStatement
    • Selecting Data using JDBC
    • Inserting Data using JDBC
    • Updating Data using JDBC
    • Deleting Data using JDBC
    • Invoking Function and Stored Procedure using JDBC
  • Lambda
    • Introduction to Lambda Expressions
    • Functional Interface
    • Filtering, Mapping, Reducing
    • Lambda Expressions in Collections
    • Method References
    • Functional Programming Concepts
    • Stream API
    • Error Handling in Lambda Expressions
    • Optional in Functional Programming
    • Parallel Processing with Lambda
    • Functional Programming Patterns
    • Advanced Topics in Lambda Expressions
    • Best Practices and Design Patterns
    • Real-World Use Cases and Examples
Powered by GitBook
On this page
  1. Basic Java

Variadic Function

In Java, variadic functions allow you to pass a variable number of arguments to a method. Unlike regular methods, which have a fixed number of parameters, variadic functions can accept a varying number of arguments. This flexibility is achieved using an ellipsis (...) in the method parameter list. Here's how variadic functions work in Java:

Syntax for Variadic Function:

return_type methodName(data_type... variableName) {
    // Method implementation
}
  • return_type: Specifies the data type the method returns.

  • methodName: Specifies the name of the variadic function.

  • data_type... variableName: Indicates a variable number of parameters of the specified data type.

  • Inside the method, you can treat variableName as an array of the specified data type.

Example of Variadic Function:

public class VariadicFunctionExample {

    // Variadic function to calculate the sum of integers
    public static int calculateSum(int... numbers) {
        int sum = 0;
        for (int num : numbers) {
            sum += num;
        }
        return sum;
    }

    public static void main(String[] args) {
        int sum1 = calculateSum(1, 2, 3, 4, 5);
        int sum2 = calculateSum(10, 20, 30);

        System.out.println("Sum 1: " + sum1);
        System.out.println("Sum 2: " + sum2);
    }
}

Explanation:

  1. public static int calculateSum(int... numbers) { ... }: This line defines a variadic function named calculateSum. It accepts a variable number of integers (numbers) as arguments.

  2. for (int num : numbers) { sum += num; }: In the method body, the function iterates over the numbers array (which is treated as an array due to the variadic parameter) and calculates the sum of all integers passed to the function.

  3. int sum1 = calculateSum(1, 2, 3, 4, 5);: This line calls the calculateSum function with five integers as arguments and assigns the returned sum to the variable sum1.

  4. int sum2 = calculateSum(10, 20, 30);: This line calls the calculateSum function with three integers as arguments and assigns the returned sum to the variable sum2.

  5. System.out.println("Sum 1: " + sum1);: Prints the sum of the first set of integers.

  6. System.out.println("Sum 2: " + sum2);: Prints the sum of the second set of integers.

When you run this Java program, it will output:

Sum 1: 15
Sum 2: 60

This output demonstrates the usage of variadic functions to handle different numbers of arguments, providing flexibility in method calls.

PreviousFunctionNextEnums

Last updated 1 year ago