Table of Contents
Syntax
How does varargs works in Java?
Examples to Implement Java varargs
Example #3
Conclusion
Home Java javaTutorial Java varargs

Java varargs

Aug 30, 2024 pm 03:51 PM
java

The Java varargs is a Java Programming Language concept that simplifies the concept of creating the methods that are actually needed to take some variable number of arguments. This feature/concept is called as varargs, and varargs is the short name for the variable-length arguments. Here a method that will take some variable number of the arguments is a varargs method. Prior to the JDK 5 version, varargs(variable-length arguments) can be used to handle in two ways. One way is by using the overloaded method and the second way is by putting the arguments into an array, and this passing array to the method is done.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Syntax

public static void fun(int …a1)
{
//method body1
}
Copy after login

Explanation: Here, the fun() function is created with the parameter “int …a1”. Then the method body will be entered to use at the time of execution. One should use only one variable arg-arguments in the specific method. The vararg – variable-length argument must be the last argument; if not, compile might occur.

How does varargs works in Java?

The varargs (Variable Length Arguments) of the Java Programming Language will help simplify the creation of methods that actually take a variable number of arguments. A Java method that takes a variable number of args-arguments is called a varargs method. It is in the JDK5 version. Prior to the JDK 5 version/versions, variable-length args-arguments will handle in two ways. The first method is with the help of the overloaded method, and another way is putting args-arguments into a specific array, and then this array will be passed to the specific method. Most of the time, both are somewhat potentially error-prone, and it is going to require more and more code. The varargs concept works by allowing the method which actually accepts multiple arguments or none.

The “…” syntax will work by telling the compiler that varargs is used with these args-arguments that they should be stored in a specific array which is referred to by a1.

The vararg-variable arguments/Variable-Length Arguments methods may overload, but the overloading leads to the specific ambiguity. From the prior JDK 5 versions, varargs- Variable Length Arguments can be or can also handled in two types of ways. The first way is by overloading, and the second way is by using the array arg-argument. There will be only one type of variable argument in a method. The varargs(Variable arguments) will be or must the last argument.

There are Erroneous varargs too. One is by specifying the two varargs in only one single method. The second is by specifying the method’s first parameter instead of the last parameter.

The method can have a variable’s length parameter with some other different parameters too, but one has to ensure that only one vararg parameter should exist, and it should be written in the parameter the last section of the method declaration.

Examples to Implement Java varargs

Below are some examples mentioned:

Example #1

This is the implementation of varargs using Java Programming Language. At first, a class “Test11” is created, and then fun1() is created with varargs syntax. Then system.out.the print() function is used with the length calculating syntax “a1.length”, which is nothing but the number of elements of the array calculation. Then FOR-EACH loop is created to know each and every element of a particular array. Then system.out.the print() function is used just to print the elements of each array. Then system.out.println() is used just for the line break in the output of the command prompt. Then “Public static void main(String args[])” is created to mention the array elements inside of the specific function “fun1()”. Three different types of array elements which are with different length. Check out the output in the output section below.

Code:

class Test11
{
static void fun1(int ...a1)
{
System.out.println("The Number of the arguments metioned: " + a1.length);
for (int i1: a1)
System.out.print(i1 + " ");
System.out.println();
}
public static void main(String args[])
{
fun1(1000);
fun1(11, 12, 13, 14);
fun1();
}
}
Copy after login

Output:

Java varargs

Example #2

This is the example of a Java Program with the varargs concept implementation with just normal arguments. Here at first, a class “Test21” is created with a new function “, fun21” with two different types of arguments/parameters. Inside of the fun21() function, system.out.print() is created 2 times. The first creation is to print the str2 value, and then the second creation is to print the length of the array. Then the for-each loop is created to extract the elements of an array with different types and different numbers of elements. With the FOR loop’s help, only the numerical arguments/values will be printed, and the string elements will be printed differently.

Code:

class Test21
{
static void fun21(String str2, int ...a2)
{
System.out.println("String: " + str2);
System.out.println("Number of arguments is: "+ a2.length);
for (int i2: a2)
System.out.print(i2 + " ");
System.out.println();
}
public static void main(String args[])
{
fun21("PavanKumarSakeCEOofProfitLoops", 1000, 2000);
fun21("ProfitLoops", 21, 22, 23, 24, 25);
fun21("fortheWorld");
}
}
Copy after login

Output:

Java varargs

Example #3

This is an example of implementing a varargs program in Java code. Here at first, Test 21 class is created, then the display () static function is created. Then inside of it, system.out.println() is used to print the string element. Then the for-each concept is used to print all the elements of the array. Then the parenthesis closing is done for the function display(). Then “public static void main(String_args[])” is used for entering the code, which actually returns the output. Then the display() is used with different types of array elements with different lengths.

Code:

class Test21{
static void display(String... values1){
System.out.println("Here Now the display method is invoked ");
for(String s1:values1){
System.out.println(s1);
}
}
public static void main(String args[]){
display();
display("Howdyhello");
display("my","name","is","varargs-pavan");
display("Pavan Kumar Sake is the future billionaire. Soon You will see.. :-)");
}
}
Copy after login

Output:

Java varargs

Conclusion

I hope you learned the actual definition of Java varargs (Variable Length Arguments) along with their syntax and explanation, How the varargs works in the Java Programming Language, and the various examples of implementing the varargs concept to understand better.

The above is the detailed content of Java varargs. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Square Root in Java Square Root in Java Aug 30, 2024 pm 04:26 PM

Guide to Square Root in Java. Here we discuss how Square Root works in Java with example and its code implementation respectively.

Perfect Number in Java Perfect Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Random Number Generator in Java Random Number Generator in Java Aug 30, 2024 pm 04:27 PM

Guide to Random Number Generator in Java. Here we discuss Functions in Java with examples and two different Generators with ther examples.

Weka in Java Weka in Java Aug 30, 2024 pm 04:28 PM

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Smith Number in Java Smith Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

Java Spring Interview Questions Java Spring Interview Questions Aug 30, 2024 pm 04:29 PM

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Break or return from Java 8 stream forEach? Break or return from Java 8 stream forEach? Feb 07, 2025 pm 12:09 PM

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

TimeStamp to Date in Java TimeStamp to Date in Java Aug 30, 2024 pm 04:28 PM

Guide to TimeStamp to Date in Java. Here we also discuss the introduction and how to convert timestamp to date in java along with examples.

See all articles