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

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

About the author

Eid Aldoukani wrote 129 articles on this blog.

My name is Ead AlDoukanje - Eadoking - 3edoOo Studying IT Engineering ( Software Engineering ) You cab contact me to : email1 : eadoking@yahoo.com email2: eadoking@hotmail.com mobile : ( +963 ) 0932728084 Programming Skills : Java, C# , C++ , Java Server Pages (JSP ) with SERVLETS, PHP ,HTML , CSS ,JAVA SCRIPT with Ajax ... Is It Enough

عزيزي الزائر : اذا اعجبك المقال فبامكانك مشاركته مع زملائك عبر المواقع التالية

Dear Visitor : if you like this article you can share it with you friends throught these sites

  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • BlinkList
  • DZone
  • email
  • Fleck
  • FriendFeed
  • FSDaily
  • HelloTxt
  • Hyves
  • LinkArena
  • MSN Reporter
  • MySpace
  • Netvibes
  • Netvouz
  • Ping.fm
  • Reddit
  • Scoopeo
  • Twitter
  • Webnews.de
  • Wykop
  • Yahoo! Bookmarks
  • Yahoo! Buzz
  • Yigg

If u like this post you may like


blog comments powered by Disqus