• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Need Help

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could someone please explain me how boxing and var - args work...
 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Madan,

Welcome to JavaRanch. First of all, please use a Meaningful Subject Line when posting a question again.

Now read the topics below. You will get an answer for your question:

https://coderanch.com/t/417852/java-programmer-SCJP/certification/Var-Args

https://coderanch.com/t/269788/java-programmer-SCJP/certification/boxing-vs-var-args

https://coderanch.com/t/264482/java-programmer-SCJP/certification/var-arg-Boxing-Widening-ambiguous
 
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just to give an idea -

1. through var-args you can send zero as well as more than zero argument of same type to a method which has only one parameter(), i.e. var-arg parameter. a method can have only one var-arg parameter and it should be the last parameter if there are more than one parameter for that method.

2. boxing is used to have the primitive specific behavior in a wrapper class object.
 
Madan Mohan
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Raj,

Could you please explain me this paragraph with some example..I am not able to get it

"through var-args you can send zero as well as more than zero argument of same type to a method which has only one parameter(), i.e. var-arg parameter. a method can have only one var-arg parameter and it should be the last parameter if there are more than one parameter for that method."
 
Rajshekhar Paul
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sure.



Hope, it helps you.
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let me also try to explain you what raj meant

void method(String... args, int... i) //not allowed, cannot have more than one var-args in a method

-------------------------------------------------------------------------

void method(String... args, int i) //not allowed, var-arg must be the last parameter so the correct code would be

void method(int i, String... args)

-------------------------------------------------------------------------
void method(String... args)

now you can call this method as

method(); //no arguments
method("a"); //1 argument
method("a", "b"); //2 argument

and so on. i.e. you can pass any number of arguments to this method...

[Edit: Oops! I was not quick enough]
[ December 05, 2008: Message edited by: Ankit Garg ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic