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
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-...
Comments
Post a Comment