• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • Core Java
  • Design Patterns
  • JSP
  • Servlets
  • Building Tools
  • jQuery
  • Spring
  • Hibernate
  • Mongo DB
  • More
    • HTML
    • SCJP
    • AJAX
    • UML
    • Struts
    • J2EE
    • Testing
    • Angular JS

J2EE Reference

  • Home
  • About Us
    • Java Learning Centers
  • Contact Us

varargs in Java

July 3, 2012 By j2eereference 1 Comment

Variable arguments(Varargs)

Varargs is Variable arguments, this feature has been added in Java 5. This is the feature that enables methods to receive variable numbers of arguments

Syntax for Varargs:

public void displayVar(int count, String… args) { }

The Varargs includes three dots … which is called ellipses.

The three periods after the parameter’s type indicate that the argument may be passed as an array .

Conditions for using Varargs:

  • Varargs should come last in the parameter list.

int sum(int…a,floatc) // will result in compile time error as Varargs should be last argument.

  • Datatype of Varargs should be same.
  • Only one set of Varargs is allowed .

int sum(int…a,int…b) // compile time error, as there is only one Variable argument is allowed.

Example for Varargs:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package com.j2eereference.varArgs;
 
public class VarargsExample {
 
        public static void Vardisplay(int id, String... Varargs) {
                System.out.println("Author Id is "+id);
        for(String arg: Varargs) {       
            System.out.println(arg);
        }
        System.out.println("******************");
    }
 
    public static void main(String args[]) {
        Vardisplay(1, "Name 1 ", "25");
        Vardisplay(2, "Name 2 ", "26", "India");
    }
 
}

 Output

Author Id is 1
Name 1
25
******************
Author Id is 2
Name 2
26
India
******************

Varargs Overloading :

We Can do Overloading in Varargs.

Consider the example in this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.j2eereference.varArgs;
public class Overloading {
          static void vardisplay(int...vargs) {
          System.out.println("Inside method with int Varargs");
          System.out.println("Length of arguments: " +vargs.length);
           for (int arg : vargs)
             {
              System.out.println("Value "+arg);
             }
           }
 
          static void vardisplay(String... vargs) {
            System.out.println("Inside method with String varargs");
            System.out.println("Length of arguments: " +vargs.length);
            for (String arg : vargs)
              System.out.println(arg);
        }
          public static void main(String args[]) {
            vardisplay(1, 2); // will compile and run
           //vardisplay(); // Give compile time error.
            vardisplay("Delhi","Calcutta", "Banglore"); // will compile and run
 
          }
   }

Output

Inside method with int Varargs
Length of arguments: 2
Value 1
Value 2
Inside method with String varargs
Length of arguments: 3
Delhi
Calcutta
Banglore

How To covert Varargs into array.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.j2eereference.varargs;
 
public class VarargsDemo
{
public static void varDisplay(int...args)
{
int[] vartoarray=args;
System.out.println("Elements in array is:");
for (int i = 0; i < args.length; i++) {
int arr=vartoarray[i];
System.out.println(arr);
    }
}
 
public static void main(String[] args) {
varDisplay(100,101,102,103,104,105,106);
  }
 
}

Output is:Elements in array is:

100

101

102

103

104

105

106

Related Posts

  • Busy Spinning in mutithreading
  • Implementing queue in java
  • TreeSet vs ConcurrentSkipListSet
  • How to create immutable class?
  • Implementing Stack in java
  • What is ReentrantLock?
  • What is Semaphore in java
  • Why AtomicInteger class
  • What is CyclicBarrier?
  • CountDownLatch in java

Filed Under: Core Java

Reader Interactions

Trackbacks

  1. ProcessBuilder - j2eereference.com says:
    April 28, 2015 at 12:09 am

    […] the second case we are passing parameter through varargs , Click here to know more about varargs in […]

    Reply

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

FOLLOW US ONLINE

  • View J2eereference-166104970118637’s profile on Facebook
  • View j2eereference’s profile on Twitter
  • View j2eereference’s profile on LinkedIn

Subscribe by email

Recent posts

  • What is parallel Stream
  • reduce method of the Stream class
  • Difference between the findFirst() and findAny() method
  • intern() method of String class
  • SOLID – Five principles of object-oriented software design
  • Java Coding Best Practices
  • How to use lambda expression effectively
  • Enhanced pseudo-Random Number Generators in java17
  • How to use Foreign-Memory Access API
  • Pattern Matching for instanceof
  • Text Blocks – Feature added in Java17
  • Record – The new feature added in java 17
  • What is Sealed Class
  • Features added in Java 17
  • Java Buzzwords

Footer

Core Java
Design Patterns
JSP
Servlets
HTML
Building Tools
AJAX
SCJP
jQuery
Testing
Spring
UML
Struts
Java Centers
Java Training
Home
About Us
Contact Us
Copyright © j2eereference.com. All right reserved.