SlideShare une entreprise Scribd logo
1  sur  12
Télécharger pour lire hors ligne
 Var-arg methods stands for Variable Number of Argument Methods.
Before var-arg either we use overloaded method or take an array
as the method parameter but it was not considered good because it
leads to the maintenance problem.
 Until 1.4v we can’t declare a method with variable number of
arguments. If there is a change in number of arguments,
compulsory we should go for new method. The drawback of this
approach is increased in length of the code & reduced readability.
 To overcome this problem SUN microsystems’s people introduced
var-arg methods in 1.5v. According to this we can declare a method
which can take variable number of argument, such type of methods
are called var-arg methods.
class Test
{
public static void method1(int x)
{
System.out.println("single int-arg method
invoked.");
}
public static void method1(int x, int y)
{
System.out.println("double int-arg method invoked.");
}
public static void main(String[] args)
{
method1(10);
method1(10,20);
}
}
Output:
single int-arg
method
invoked.
double int-arg
method
invoked.
class Test1
{
public static void method1(int... x)
{
System.out.println("var-arg method
invoked.");
}
public static void main(String[] args)
{
method1(10);
method1(10,20);
}
}
Output:
var-arg method
invoked.
var-arg method
invoked.
 m1(int… x)
 m1(int …x)
 m1(int…x)
 m1(int x…)
 m1(int. ..x)
 m1(int. x...)
 m1(int x, int… y)
 m1(String s, double… d)
 m1(int… x, int y)
 m1(String… s, double… d)
class Test
{
public static void m1(int... x)
{
System.out.println("var-arg method.");
}
public static void m1(int[] x)
{
System.out.println("general method.");
}
public static void main(String[] args)
{
method1();
}
}
CE:
cannot declare
both m1(int[])
and m1(int...) in
Test
class Test1
{
public static void m1(int... x)
{
System.out.println("var-arg method.");
}
public static void m1(int x)
{
System.out.println("general method.");
}
public static void main(String[] args)
{
m1();
m1(10,20);
m1(10);
}
}
Output:
var-arg method.
var-arg method.
general method.
class Test1
{
void varArg(int... x)
{
System.out.println(x[1]);
}
public static void main(String[] args)
{
Test t = new Test();
t.varArg(10,20,30,40,50);
}
}
Output:
20
The main advantage of var-arg method is, we don’t need to use
overloaded method. So, the length of the code will reduce.
If we don't know how many argument we will have to pass in the
method, var-arg method is the better approach.
Var arg methods

Contenu connexe

Tendances (20)

Methods in Java
Methods in JavaMethods in Java
Methods in Java
 
Features of java
Features of javaFeatures of java
Features of java
 
Presentation on-exception-handling
Presentation on-exception-handlingPresentation on-exception-handling
Presentation on-exception-handling
 
Exception Handling in JAVA
Exception Handling in JAVAException Handling in JAVA
Exception Handling in JAVA
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Exception Handling
Exception HandlingException Handling
Exception Handling
 
Ppt on this and super keyword
Ppt on this and super keywordPpt on this and super keyword
Ppt on this and super keyword
 
Perl Scripting
Perl ScriptingPerl Scripting
Perl Scripting
 
9. Input Output in java
9. Input Output in java9. Input Output in java
9. Input Output in java
 
Java input
Java inputJava input
Java input
 
Java(Polymorphism)
Java(Polymorphism)Java(Polymorphism)
Java(Polymorphism)
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Strings in Java
Strings in JavaStrings in Java
Strings in Java
 
Java I/o streams
Java I/o streamsJava I/o streams
Java I/o streams
 
Method overloading
Method overloadingMethod overloading
Method overloading
 
Package in Java
Package in JavaPackage in Java
Package in Java
 
Java IO
Java IOJava IO
Java IO
 
Hashing
HashingHashing
Hashing
 
Files in c++
Files in c++Files in c++
Files in c++
 
Structure
StructureStructure
Structure
 

En vedette

Delicuencia en valle de bravo
Delicuencia en valle de bravoDelicuencia en valle de bravo
Delicuencia en valle de bravoCarmenSocio
 
Mobile Success Case: Caixagest App
Mobile Success Case: Caixagest AppMobile Success Case: Caixagest App
Mobile Success Case: Caixagest AppXpand IT
 
Powerful Backends with Microsoft Azure
Powerful Backends with Microsoft AzurePowerful Backends with Microsoft Azure
Powerful Backends with Microsoft AzurePierce Boggan
 
แบบเสนอโครงร่างโครงงานคอมพิวเตอร์
แบบเสนอโครงร่างโครงงานคอมพิวเตอร์แบบเสนอโครงร่างโครงงานคอมพิวเตอร์
แบบเสนอโครงร่างโครงงานคอมพิวเตอร์มัลลิกา คมอ่อน
 
Знову Declined by Client? Це ще не кінець! (Христина Монець)
Знову Declined by Client? Це ще не кінець! (Христина Монець)Знову Declined by Client? Це ще не кінець! (Христина Монець)
Знову Declined by Client? Це ще не кінець! (Христина Монець)BeTWinCon
 
Presentació dreamslab
Presentació   dreamslabPresentació   dreamslab
Presentació dreamslabjoanaliau22
 
Martin Lindstrom - Small Data - full day content for part 3 of 4 handout
Martin Lindstrom - Small Data - full day content for part 3 of 4 handoutMartin Lindstrom - Small Data - full day content for part 3 of 4 handout
Martin Lindstrom - Small Data - full day content for part 3 of 4 handoutMarian Costache
 
ใบงานแบบสำรวจและประวัติ
ใบงานแบบสำรวจและประวัติใบงานแบบสำรวจและประวัติ
ใบงานแบบสำรวจและประวัติPatipan Punyakaew
 
ข้อสอบโควตา มช 2555 คณิต
ข้อสอบโควตา มช 2555 คณิตข้อสอบโควตา มช 2555 คณิต
ข้อสอบโควตา มช 2555 คณิตGe Ar
 
Presentació eportafolis IOC
Presentació eportafolis IOCPresentació eportafolis IOC
Presentació eportafolis IOCJoan Queralt
 
El Portfolio como herramienta de evaluación
El Portfolio como herramienta de evaluaciónEl Portfolio como herramienta de evaluación
El Portfolio como herramienta de evaluaciónRosa Liarte Alcaine
 
โครงร่างโครงงาน
โครงร่างโครงงานโครงร่างโครงงาน
โครงร่างโครงงานAngkana Potha
 

En vedette (17)

Delicuencia en valle de bravo
Delicuencia en valle de bravoDelicuencia en valle de bravo
Delicuencia en valle de bravo
 
Mobile Success Case: Caixagest App
Mobile Success Case: Caixagest AppMobile Success Case: Caixagest App
Mobile Success Case: Caixagest App
 
Powerful Backends with Microsoft Azure
Powerful Backends with Microsoft AzurePowerful Backends with Microsoft Azure
Powerful Backends with Microsoft Azure
 
แบบเสนอโครงร่างโครงงานคอมพิวเตอร์
แบบเสนอโครงร่างโครงงานคอมพิวเตอร์แบบเสนอโครงร่างโครงงานคอมพิวเตอร์
แบบเสนอโครงร่างโครงงานคอมพิวเตอร์
 
Знову Declined by Client? Це ще не кінець! (Христина Монець)
Знову Declined by Client? Це ще не кінець! (Христина Монець)Знову Declined by Client? Це ще не кінець! (Христина Монець)
Знову Declined by Client? Це ще не кінець! (Христина Монець)
 
Presentació dreamslab
Presentació   dreamslabPresentació   dreamslab
Presentació dreamslab
 
CardTech_SESAMES_F[1]
CardTech_SESAMES_F[1]CardTech_SESAMES_F[1]
CardTech_SESAMES_F[1]
 
Retazos
RetazosRetazos
Retazos
 
Concurso logo escuelas sostenibles
Concurso logo  escuelas  sosteniblesConcurso logo  escuelas  sostenibles
Concurso logo escuelas sostenibles
 
Martin Lindstrom - Small Data - full day content for part 3 of 4 handout
Martin Lindstrom - Small Data - full day content for part 3 of 4 handoutMartin Lindstrom - Small Data - full day content for part 3 of 4 handout
Martin Lindstrom - Small Data - full day content for part 3 of 4 handout
 
ใบงานแบบสำรวจและประวัติ
ใบงานแบบสำรวจและประวัติใบงานแบบสำรวจและประวัติ
ใบงานแบบสำรวจและประวัติ
 
MISMO / eMortgage Update
MISMO / eMortgage UpdateMISMO / eMortgage Update
MISMO / eMortgage Update
 
ข้อสอบโควตา มช 2555 คณิต
ข้อสอบโควตา มช 2555 คณิตข้อสอบโควตา มช 2555 คณิต
ข้อสอบโควตา มช 2555 คณิต
 
Presentació eportafolis IOC
Presentació eportafolis IOCPresentació eportafolis IOC
Presentació eportafolis IOC
 
Pat2 พ.ย. 57
Pat2 พ.ย. 57Pat2 พ.ย. 57
Pat2 พ.ย. 57
 
El Portfolio como herramienta de evaluación
El Portfolio como herramienta de evaluaciónEl Portfolio como herramienta de evaluación
El Portfolio como herramienta de evaluación
 
โครงร่างโครงงาน
โครงร่างโครงงานโครงร่างโครงงาน
โครงร่างโครงงาน
 

Similaire à Var arg methods

Lecture5_Method_overloading_Final power point presentation
Lecture5_Method_overloading_Final power point presentationLecture5_Method_overloading_Final power point presentation
Lecture5_Method_overloading_Final power point presentationbhargavi804095
 
Lecture4_Method_overloading power point presentaion
Lecture4_Method_overloading power point presentaionLecture4_Method_overloading power point presentaion
Lecture4_Method_overloading power point presentaionbhargavi804095
 
CIS 1403 lab 3 functions and methods in Java
CIS 1403 lab 3 functions and methods in JavaCIS 1403 lab 3 functions and methods in Java
CIS 1403 lab 3 functions and methods in JavaHamad Odhabi
 
TestNG - The Next Generation of Unit Testing
TestNG - The Next Generation of Unit TestingTestNG - The Next Generation of Unit Testing
TestNG - The Next Generation of Unit TestingBethmi Gunasekara
 
Java findamentals2
Java findamentals2Java findamentals2
Java findamentals2Todor Kolev
 
Java findamentals2
Java findamentals2Java findamentals2
Java findamentals2Todor Kolev
 
Md06 advance class features
Md06 advance class featuresMd06 advance class features
Md06 advance class featuresRakesh Madugula
 
9781439035665 ppt ch07
9781439035665 ppt ch079781439035665 ppt ch07
9781439035665 ppt ch07Terry Yoast
 
9781111530532 ppt ch07
9781111530532 ppt ch079781111530532 ppt ch07
9781111530532 ppt ch07Terry Yoast
 
9781111530532 ppt ch07
9781111530532 ppt ch079781111530532 ppt ch07
9781111530532 ppt ch07Terry Yoast
 
Effective Java - Design Method Signature Overload Varargs
Effective Java - Design Method Signature Overload VarargsEffective Java - Design Method Signature Overload Varargs
Effective Java - Design Method Signature Overload VarargsRoshan Deniyage
 

Similaire à Var arg methods (20)

Lecture5_Method_overloading_Final power point presentation
Lecture5_Method_overloading_Final power point presentationLecture5_Method_overloading_Final power point presentation
Lecture5_Method_overloading_Final power point presentation
 
Lecture4_Method_overloading power point presentaion
Lecture4_Method_overloading power point presentaionLecture4_Method_overloading power point presentaion
Lecture4_Method_overloading power point presentaion
 
CIS 1403 lab 3 functions and methods in Java
CIS 1403 lab 3 functions and methods in JavaCIS 1403 lab 3 functions and methods in Java
CIS 1403 lab 3 functions and methods in Java
 
TestNG - The Next Generation of Unit Testing
TestNG - The Next Generation of Unit TestingTestNG - The Next Generation of Unit Testing
TestNG - The Next Generation of Unit Testing
 
Core java
Core javaCore java
Core java
 
Java findamentals2
Java findamentals2Java findamentals2
Java findamentals2
 
Java findamentals2
Java findamentals2Java findamentals2
Java findamentals2
 
Md06 advance class features
Md06 advance class featuresMd06 advance class features
Md06 advance class features
 
Ppt chapter04
Ppt chapter04Ppt chapter04
Ppt chapter04
 
Pptchapter04
Pptchapter04Pptchapter04
Pptchapter04
 
9781439035665 ppt ch07
9781439035665 ppt ch079781439035665 ppt ch07
9781439035665 ppt ch07
 
14 interface
14  interface14  interface
14 interface
 
M C6java3
M C6java3M C6java3
M C6java3
 
Java
JavaJava
Java
 
9781111530532 ppt ch07
9781111530532 ppt ch079781111530532 ppt ch07
9781111530532 ppt ch07
 
9781111530532 ppt ch07
9781111530532 ppt ch079781111530532 ppt ch07
9781111530532 ppt ch07
 
Suga java training_with_footer
Suga java training_with_footerSuga java training_with_footer
Suga java training_with_footer
 
Core & Advance Java Training For Beginner-PSK Technologies Pvt. Ltd. Nagpur
Core & Advance Java Training For Beginner-PSK Technologies Pvt. Ltd. NagpurCore & Advance Java Training For Beginner-PSK Technologies Pvt. Ltd. Nagpur
Core & Advance Java Training For Beginner-PSK Technologies Pvt. Ltd. Nagpur
 
Effective Java - Design Method Signature Overload Varargs
Effective Java - Design Method Signature Overload VarargsEffective Java - Design Method Signature Overload Varargs
Effective Java - Design Method Signature Overload Varargs
 
Methods.ppt
Methods.pptMethods.ppt
Methods.ppt
 

Dernier

Scientific Writing :Research Discourse
Scientific  Writing :Research  DiscourseScientific  Writing :Research  Discourse
Scientific Writing :Research DiscourseAnita GoswamiGiri
 
Satirical Depths - A Study of Gabriel Okara's Poem - 'You Laughed and Laughed...
Satirical Depths - A Study of Gabriel Okara's Poem - 'You Laughed and Laughed...Satirical Depths - A Study of Gabriel Okara's Poem - 'You Laughed and Laughed...
Satirical Depths - A Study of Gabriel Okara's Poem - 'You Laughed and Laughed...HetalPathak10
 
Objectives n learning outcoms - MD 20240404.pptx
Objectives n learning outcoms - MD 20240404.pptxObjectives n learning outcoms - MD 20240404.pptx
Objectives n learning outcoms - MD 20240404.pptxMadhavi Dharankar
 
ICS 2208 Lecture Slide Notes for Topic 6
ICS 2208 Lecture Slide Notes for Topic 6ICS 2208 Lecture Slide Notes for Topic 6
ICS 2208 Lecture Slide Notes for Topic 6Vanessa Camilleri
 
Congestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentationCongestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentationdeepaannamalai16
 
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptx
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptxMan or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptx
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptxDhatriParmar
 
Mythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITWMythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITWQuiz Club NITW
 
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...Association for Project Management
 
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...Nguyen Thanh Tu Collection
 
Comparative Literature in India by Amiya dev.pptx
Comparative Literature in India by Amiya dev.pptxComparative Literature in India by Amiya dev.pptx
Comparative Literature in India by Amiya dev.pptxAvaniJani1
 
Q-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITWQ-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITWQuiz Club NITW
 
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQ-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQuiz Club NITW
 
Indexing Structures in Database Management system.pdf
Indexing Structures in Database Management system.pdfIndexing Structures in Database Management system.pdf
Indexing Structures in Database Management system.pdfChristalin Nelson
 
CLASSIFICATION OF ANTI - CANCER DRUGS.pptx
CLASSIFICATION OF ANTI - CANCER DRUGS.pptxCLASSIFICATION OF ANTI - CANCER DRUGS.pptx
CLASSIFICATION OF ANTI - CANCER DRUGS.pptxAnupam32727
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...Nguyen Thanh Tu Collection
 
Shark introduction Morphology and its behaviour characteristics
Shark introduction Morphology and its behaviour characteristicsShark introduction Morphology and its behaviour characteristics
Shark introduction Morphology and its behaviour characteristicsArubSultan
 
DBMSArchitecture_QueryProcessingandOptimization.pdf
DBMSArchitecture_QueryProcessingandOptimization.pdfDBMSArchitecture_QueryProcessingandOptimization.pdf
DBMSArchitecture_QueryProcessingandOptimization.pdfChristalin Nelson
 

Dernier (20)

Scientific Writing :Research Discourse
Scientific  Writing :Research  DiscourseScientific  Writing :Research  Discourse
Scientific Writing :Research Discourse
 
Satirical Depths - A Study of Gabriel Okara's Poem - 'You Laughed and Laughed...
Satirical Depths - A Study of Gabriel Okara's Poem - 'You Laughed and Laughed...Satirical Depths - A Study of Gabriel Okara's Poem - 'You Laughed and Laughed...
Satirical Depths - A Study of Gabriel Okara's Poem - 'You Laughed and Laughed...
 
Objectives n learning outcoms - MD 20240404.pptx
Objectives n learning outcoms - MD 20240404.pptxObjectives n learning outcoms - MD 20240404.pptx
Objectives n learning outcoms - MD 20240404.pptx
 
ICS 2208 Lecture Slide Notes for Topic 6
ICS 2208 Lecture Slide Notes for Topic 6ICS 2208 Lecture Slide Notes for Topic 6
ICS 2208 Lecture Slide Notes for Topic 6
 
Congestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentationCongestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentation
 
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptx
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptxMan or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptx
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptx
 
Mythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITWMythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITW
 
Plagiarism,forms,understand about plagiarism,avoid plagiarism,key significanc...
Plagiarism,forms,understand about plagiarism,avoid plagiarism,key significanc...Plagiarism,forms,understand about plagiarism,avoid plagiarism,key significanc...
Plagiarism,forms,understand about plagiarism,avoid plagiarism,key significanc...
 
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
 
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
 
Comparative Literature in India by Amiya dev.pptx
Comparative Literature in India by Amiya dev.pptxComparative Literature in India by Amiya dev.pptx
Comparative Literature in India by Amiya dev.pptx
 
Q-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITWQ-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITW
 
Spearman's correlation,Formula,Advantages,
Spearman's correlation,Formula,Advantages,Spearman's correlation,Formula,Advantages,
Spearman's correlation,Formula,Advantages,
 
Faculty Profile prashantha K EEE dept Sri Sairam college of Engineering
Faculty Profile prashantha K EEE dept Sri Sairam college of EngineeringFaculty Profile prashantha K EEE dept Sri Sairam college of Engineering
Faculty Profile prashantha K EEE dept Sri Sairam college of Engineering
 
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQ-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
 
Indexing Structures in Database Management system.pdf
Indexing Structures in Database Management system.pdfIndexing Structures in Database Management system.pdf
Indexing Structures in Database Management system.pdf
 
CLASSIFICATION OF ANTI - CANCER DRUGS.pptx
CLASSIFICATION OF ANTI - CANCER DRUGS.pptxCLASSIFICATION OF ANTI - CANCER DRUGS.pptx
CLASSIFICATION OF ANTI - CANCER DRUGS.pptx
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...
 
Shark introduction Morphology and its behaviour characteristics
Shark introduction Morphology and its behaviour characteristicsShark introduction Morphology and its behaviour characteristics
Shark introduction Morphology and its behaviour characteristics
 
DBMSArchitecture_QueryProcessingandOptimization.pdf
DBMSArchitecture_QueryProcessingandOptimization.pdfDBMSArchitecture_QueryProcessingandOptimization.pdf
DBMSArchitecture_QueryProcessingandOptimization.pdf
 

Var arg methods

  • 1.
  • 2.  Var-arg methods stands for Variable Number of Argument Methods. Before var-arg either we use overloaded method or take an array as the method parameter but it was not considered good because it leads to the maintenance problem.  Until 1.4v we can’t declare a method with variable number of arguments. If there is a change in number of arguments, compulsory we should go for new method. The drawback of this approach is increased in length of the code & reduced readability.  To overcome this problem SUN microsystems’s people introduced var-arg methods in 1.5v. According to this we can declare a method which can take variable number of argument, such type of methods are called var-arg methods.
  • 3. class Test { public static void method1(int x) { System.out.println("single int-arg method invoked."); } public static void method1(int x, int y) { System.out.println("double int-arg method invoked."); } public static void main(String[] args) { method1(10); method1(10,20); } } Output: single int-arg method invoked. double int-arg method invoked.
  • 4. class Test1 { public static void method1(int... x) { System.out.println("var-arg method invoked."); } public static void main(String[] args) { method1(10); method1(10,20); } } Output: var-arg method invoked. var-arg method invoked.
  • 5.
  • 6.  m1(int… x)  m1(int …x)  m1(int…x)  m1(int x…)  m1(int. ..x)  m1(int. x...)
  • 7.  m1(int x, int… y)  m1(String s, double… d)  m1(int… x, int y)  m1(String… s, double… d)
  • 8. class Test { public static void m1(int... x) { System.out.println("var-arg method."); } public static void m1(int[] x) { System.out.println("general method."); } public static void main(String[] args) { method1(); } } CE: cannot declare both m1(int[]) and m1(int...) in Test
  • 9. class Test1 { public static void m1(int... x) { System.out.println("var-arg method."); } public static void m1(int x) { System.out.println("general method."); } public static void main(String[] args) { m1(); m1(10,20); m1(10); } } Output: var-arg method. var-arg method. general method.
  • 10. class Test1 { void varArg(int... x) { System.out.println(x[1]); } public static void main(String[] args) { Test t = new Test(); t.varArg(10,20,30,40,50); } } Output: 20
  • 11. The main advantage of var-arg method is, we don’t need to use overloaded method. So, the length of the code will reduce. If we don't know how many argument we will have to pass in the method, var-arg method is the better approach.