Methods with a Variable Number of Parameters java jdk 6

Good Night :

Before Java SE 5.0 every Java method had a fixed number of parameters.

However, it is now possible to provide methods that can be called with a variable number of parameters.
(These are sometimes called “varargs” methods.)
You can define your own methods with variable parameters, and you can specify any type for the parameters, even a primitive type.

Here is a simple example: a function that computes the maximum of a variable number of values

لمحة عربية سريعة

قبل ان تصدر شركة Sun الحزمة السادسة من JDK اي عندما كانت النسخة المستخدمة هي JDK 5 , كانت لكل الدوال (التوابع ) عدد محدد من المتحولات او نسطيع ان نقول عدد ثابت فقط.

ولكن من النسخة السادسة JDK 6 قامت الشركة بالسماح للتوابع ذات العدد المتغير من المتحولات وتسمى VARARGS METHODS . والتي يمكن لنا ان نقوم بانشاء طريقة تحوي على عدد متغير من المتحولات .

مثال بسيط تابع يقوم بحساب اكبر رقم بين عدد غير محدد من الارقام .

كانت هذه العملية في الاصدارات السابقة نوعا ما غير محببة اما الان فنكتب تابع واحد بسيط كما يلي .

testClassOfVarPara.java

/**
*
* @author  Eadoking
* @website http://eadoking.com
* @email   eadoking@yahoo.com
* @email   info@eadoking.com
* @mobile  +963 932728084
*/
public class testClassOfVarPara {
// integer maximum method with variable number of parameters
public static int maximumVarPara(int... numbers) {
int max = Integer.MIN_VALUE;
for (int num : numbers) {
if (num > max) {
max = num;
}
}
return max;
}
// double maximum method with variavle number of parameters
public static double maximumVarPara(double... numbers) {
double max = Double.MIN_VALUE;
for (double num : numbers) {
if (num > max) {
max = num;
}
}
return max;
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
int inrArr[] = {10, 20, 50, 63, 2, 4, 9, 5};
System.out.println("The maximum of (int arr) is : " + maximumVarPara(inrArr));
double doubleArr[] = {93.2, 23.5, 263.74, .63, 0.3, 90.5};
System.out.println("The maximum of (double arr) is : " + maximumVarPara(doubleArr));
System.out.println("You can try any number of variable");
System.out.println("You can use more complex not only int");
System.out.println("You can use also objects");
}
}

For any question , just post it in comments and i’ll explain it

لاى استفسار او اي غموض غير واضح الرجاء وضع تعليق بالجزء الغير واضح وساقوم بالتوضيح

Tags: , , , , , , , , ,

About eadoking

Ead AlDoukanje - EADOKING - 3edoOo ( عيد الدكنجي Studied Informatics Engineering at Tishreen University ( Software Engineering Department ) Work as freelancer Web Developer / Software Developer You can contact me to me@eadoking.com mobile : ( +963 ) 0932728084 Programming Skills : Java, C# , C++ , Java Server Pages (JSP ) with SERVLETS, PHP ,HTML , CSS ,JAVA SCRIPT with Ajax , ASP.NET , Web Services .

Stay in touch

subscribe with us using social media or email to stay updated
you can also contact us if you need some request or any thing to ,
http://me.eadoking.com/contact-me

Trackbacks/Pingbacks

  1. Methods with a Variable Number of Parameters Java Jdk 6 | Eadoking … - October 19, 2009

    [...] Methods with a Variable Number of Parameters java jdk 6. Written by eadoking eadoking No Comments Comments Last Updated: August 27, 2009. Good Night : Before Java SE 5.0 every Java method had a fixed number of parameters. …Page 2 [...]

  2. Methods with a Variable Number of Parameters Java Jdk 6 | Eadoking … - October 17, 2009

    [...] Methods with a Variable Number of Parameters java jdk 6. Written by eadoking eadoking No Comments Comments Last Updated: August 27, 2009. Good Night : Before Java SE 5.0 every Java method had a fixed number of parameters. …Continue Reading… [...]

Leave a Reply


*