| Author |
Need Help
|
Madan Mohan
Ranch Hand
Joined: Dec 05, 2008
Posts: 30
|
|
|
Could someone please explain me how boxing and var - args work...
|
 |
Kenneth Lomvey
Ranch Hand
Joined: Nov 08, 2008
Posts: 94
|
|
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: http://www.coderanch.com/t/417852/java-programmer-SCJP/certification/Var-Args http://www.coderanch.com/t/269788/java-programmer-SCJP/certification/boxing-vs-var-args http://www.coderanch.com/t/264482/java-programmer-SCJP/certification/var-arg-Boxing-Widening-ambiguous
|
 |
Rajshekhar Paul
Ranch Hand
Joined: Oct 17, 2006
Posts: 140
|
|
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.
|
When it's obvious that you have to do it, just do it without shattering your thoughts over different directions.
|
 |
Madan Mohan
Ranch Hand
Joined: Dec 05, 2008
Posts: 30
|
|
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
Joined: Oct 17, 2006
Posts: 140
|
|
Sure. Hope, it helps you.
|
 |
Ankit Garg
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9189
|
|
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 ]
|
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
|
 |
 |
|
|
subject: Need Help
|
|
|