Hub Of Geekz

  • Home
  • About Me
  • Contact Us
  • Home
  • Languages
    • C++
    • Java
    • Perl
    • Prolog
    • Bootstrap
  • Database
    • SQL
    • PL/SQL
  • Study
    • Java
      • Java Collection
      • Java Concurrency
      • Java Interview Questions
      • Java Networking
    • Number System
  • Kavita
  • Entertainment
    • Hinglish Cafe
    • Videos
    • Jokes
  • Windows Tricks
  • How To

Tuesday, 10 March 2015

Java Interview Question-Variable length arguments to method

 Earthcare Foundation NGO     10:07     Java, Java Interview Questions     No comments   

Do you know that we can pass a variable length argument lists in the method.This can be done by passing the datatype followed by an ellipsis(...) and then variable name.The ellipsis indicates that method can receive variable length of arguments of specific type.We can also use method overloading on this method.The ellipsis passed in the method is taken as the array of specific type.So we can apply the array operations on this variable length parameter list.
For Example:

package examples;

public class VarArguments {

public static int sum(int ... numbers){
int res=0;
for(int number:numbers){
res=res+number;
}
return res;
}

public static void main(String[] args) {
int a=12;
int b=13;
int c=14;
int d=15;
System.out.println("a="+a+"\nb="+b+"\nc="+c+"\nd="+d);
System.out.println("Sum of a and b is "+sum(a,b));
System.out.println("Sum of a,b and c is "+sum(a,b,c));
System.out.println("Sum of a,b,c and d is "+sum(a,b,c,d));

}

}
Output:
a=12
b=13
c=14
d=15
Sum of a and b is 25
Sum of a,b and c is 39
Sum of a,b,c and d is 54

In the above example i have created a method which takes a variable number of arguments.Then i have applied a for loop to get the sum of elements.In main method i have supplied 2 arguments then 3 arguments and then 4 arguments to get the sum of those numbers.
So tgis is a great thing we can apply in our programs where we don't know how much arguments have to be passed in the method which makes our problem simple.
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Email ThisBlogThis!Share to XShare to Facebook
Newer Post Older Post Home

0 comments:

Post a Comment

Ad


Jobsmag.inIndian Education BlogThingsGuide

Subscribe

Do you want fresh and hot updates about us? Then subscribe to our Feeds.

Total Pageviews

Popular Posts

  • Write a program in PL/SQL to print the factorial of a number.
    In this post I will explain how to get the factorial of any given number. For that first you need to know what is the procedure to find ...
  • To find the GCD of two numbers in PROLOG.
    gcd(X,Y):-X=Y,write('GCD of two numbers is '),write(X); X=0,write('GCD of two numbers is '),write(Y); Y=0,write('G...
  • Write a PL/SQL code to get the Fibonacci Sequence
    First, I will explain what is Fibonacci Sequence and how to get this series. So, Fibonacci Sequence is a series of numbers 0,1,1,2,3,5,8,1...

Label

All Articles Best Resources Blogging Boost Traffic Bootstrap C Plus Plus Collection Comedy Comedy Posts Comedy Videos Concurrency creative commons website Education Employee Entertainment Fibonacci Sequence free images GirlFriend Hinglish Cafe How To Image Websites Inspirational Java Java Interview Questions Java Networking Kavita Sangrah Life Lock Sreen Love Number System Patterns Perl Picture PL/SQL Plastic Engineering Programming Prolog public domain SEO Servlet Short Story Shortcut Keys Social Media Social Services SQL SuVichar Thread Traffic True Events Ultimate Guide Windows Tricks Windows8.1 WordPress

Blog Archive

  • ►  2020 (43)
    • ►  September (41)
    • ►  August (2)
  • ►  2019 (1)
    • ►  July (1)
  • ►  2018 (9)
    • ►  September (7)
    • ►  July (1)
    • ►  May (1)
  • ►  2017 (8)
    • ►  June (3)
    • ►  May (3)
    • ►  March (1)
    • ►  January (1)
  • ►  2016 (2)
    • ►  September (1)
    • ►  January (1)
  • ▼  2015 (91)
    • ►  December (1)
    • ►  November (1)
    • ►  October (6)
    • ►  May (10)
    • ▼  March (20)
      • ArrayList Methods in Java
      • ArrayList Methods in Java
      • ArrayList in Java
      • Arrays Class in Java
      • Java Interview Question-Variable length arguments ...
      • Array in Java
      • Data Hierarchy in Operating Systems
      • Synchronization in Java
      • Typecasting in Java
      • Concurrency in Java
      • Runnable in Java
      • HashMap in Java
      • Reverse of a given number using Java
      • Star Pattern printing in Java
      • Difference between fail-fast and fail-safe iterators
      • Factorial of a number in Java
      • Null in Java
      • Final in Java
      • Immutable Objects in Java
      • Thread concept in java
    • ►  February (50)
    • ►  January (3)
  • ►  2014 (339)
    • ►  December (1)
    • ►  October (55)
    • ►  September (58)
    • ►  August (94)
    • ►  July (64)
    • ►  June (67)
  • ►  2013 (34)
    • ►  August (5)
    • ►  April (29)
  • ►  2012 (20)
    • ►  November (1)
    • ►  October (15)
    • ►  September (4)

Author

  • Earthcare Foundation NGO
  • Kavita house
  • Unknown
  • Unknown

Info

Copyright © Hub Of Geekz | Powered by Blogger
Design by Hardeep Asrani | Blogger Theme by NewBloggerThemes.com | Distributed By Gooyaabi Templates