Skip to main content

Variable Arguments (Varargs) in Java

 

Variable length arguments (var args)

Do Check My Video on Varargs in java:




whenever you are not sure how many inputs are going to be provided by the user or you are not sure how many arguments that you need to take inside a method at that time you need to go for variable length arguments void sub(int... arr), ... are like single dimensional array

Example:

class Test{

public static void m1(int... arr){

System.out.println("You are in the var-args");

}

public static void m2(int[] arr){

System.out.println("You are in Array");

}

    public static void main(String[] args){

int[] a = {10,20,30};

m1(a);

m2(a);

}

}

o/p:

You are in the var-args

You are in Array


Note: in above exaple you can call the method m2 by passing only the array

but in m1 method we can call that method with array as well as m1(2,3)

so in above example

m2(6,7,8) -> Not Allowed
but in the m1(3,4,5)->allowed

case 1 : Note : JVM will give the list priority to the var- rgs method rather than the exact parameter method

Example:

class Test{

    public static void m1(int... arr){

        System.out.println("Var args");

    }

    public  static void m1(int a){

        System.out.println("Simple method");

    }

    public static void main(String[] args) {

        m1(10);

    }

}

o/p: Simple method

Case 2:

Example:
class Test{
    public static void m1(int a,int b,int c){
        System.out.println("in int param");
    }
    public static void m1(byte... b){
        System.out.println("in var args method");
    }
    public static void main(String[] args) {
        byte b = 10;
        m1(b,b,b);
    }
}
o/p: in int param

Explanation:reason for the above method is java is the backward compatible language
i.e m1(int a,int b) is the method from 1.0v of the java while var args
method is from 1.5v of java so m1(int a,int b) method will bw executed.


case 3:

In Following example the double var arg method will be executed because
the input parameter of m1(10,21,34) can't store in 1-d array

Example:
class Test{
    public static void m1(double... d){
        System.out.println("Var args mwthod");
    }
    public static void m1(int[] arr){
        System.out.println("Normal Method");
    }
    public static void main(String[] args) {
        m1(10,21,34);
    }
}
o/p:Var args mwthod

case 4 : we can take both noraml argument and var arg together

class Test{
    public static void m1(int a,int... arr){
        System.out.println(a);
        for (int i:arr){
            System.out.println(i);
        }
    }
    public static void main(String[] args) {
        m1(10,11,12,12);
    }
}
o/p:
        10
        11
        12
        12

Case 5:Inside var-args method we can take only one var-args 

parameter
if we take two or more vararg parameter then we will get compile time error

Example:
class Test{
    public static void m1(int... arr,double... arr1){
        
    }
    public static void main(String[] args) {
        m1(10,11,12,12);
    }
}

Error:Test.java:3: error: varargs parameter must be the last parameter
    public static void m1(int... arr,double... arr1)

case 6: Inside a class we can't declare var-args method and 

corresponding one-dimensional array methods simultaneously
otherwise we will get Compile time error



class Test{
    public static void m1(int... arr){
        
    }
    public static void m1(int[] arr){
        
    }
    public static void main(String[] args) {
        m1(10,11,12,12);
    }
}
Error:

Test.java:6: error: cannot declare both m1(int[]) and m1(int...) in Test
    public static void m1(int[] arr){
                                 ^

  Equivalence Between var-args and 1-D array

case 1:whenever 1-D array present we can replace with var-args parameter

           m1(int[] a) == m1(int... a)

case 2:whenever var-args param present we cant replace with 1-d array

             m1(int... a)   != m1(int[] a)

case 3: m1(int[]... a) = int[][] a

Example:


class Test{
public static void m1(int[]... arr){
for(int i=0;i<2;i++){
for(int j=0;j<3;j++){
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
}
public static void main(String[] args){
int[] arr1 = {10,20,30};
int[] arr2 = {40,50,60};
m1(arr1,arr2);
}
}
o/p:
10 20 30
40 50 60

Comments

Popular posts from this blog

Servlet Project Book Shop Application in eclipse

 Servlet Project Book Shop Application in eclipse  For explanation watch video::: Note :: In this Project you must configure web server (for example tomcat) with eclipse ide Download Bootstrap  from ::         https://getbootstrap.com/docs/4.3/getting-started/download/ Download mysql jar file from :: https://mvnrepository.com/artifact/mysql/mysql-connector-java/8.0.22 adding MySQL Connector/J jar file in eclipse ide for jdbc video :: video link : https://youtu.be/4Fyd-k3eG_I Directory Structure:: web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">   <welcome-file-list>     <welcome-file>home.html</welcome-file>   </welcome-file-list>   <display-...

JDBC basic example For Select Query

JDBC basic example For Select Query  For explanation watch video:  For Creating Table:: SQL> create table emp60(srno int,fname varchar2(10)); Table created. SQL> desc emp60;  Name                                      Null?    Type  ----------------------------------------- -------- ----------------------------  SRNO                                               NUMBER(38)  FNAME                                              VARCHAR2(10) SQL> insert into emp60 values(1,'allu'); 1 row created. SQL> insert into emp60 values(2,'vijay'); 1 row created. SQL> insert into emp60 values(3,'rajni'); 1 row created. SQL...

JDBC Program to access table data from mysql database

 import java.sql.*; class MysqlCon  { public static void main(String[] args)  { try{ Connection con = DriverManager.getConnection("jdbc:mysql:///new","root","root"); Statement st = con.createStatement(); String query = "select * from login"; ResultSet rs = st.executeQuery(query); while(rs.next()){ System.out.println(rs.getString(1)+" "+rs.getString(2)); } con.close(); }catch(SQLException e){ System.out.println("Error"); }catch(Exception e){ } } }