| Register | FAQ | Calendar | Search | Today's Posts | Mark Forums Read |
|
#1
| |||
| |||
| I have been doing some practice exams and came across this funny '. . .' in the signatuer of a method: int doIt(int... i) { return 4; } |
|
#2
| |||
| |||
| Hi, > I have been doing some practice exams and came across this funny '. . .' > in the signatuer of a method: > > int doIt(int... i) { > return 4; } You can pass one or more integers. See chapter 8.4.1 of the Java Language Specification[1] where it talks about "variable arity parameter". In the method body you treat i like it is declared an array of int. The difference to int doIt(int[] i) is that in this case whoever calls doIt has to pass an array, e.g.. call it as doIt(new int[]{1,2,3}); while in the int... case you can just call it as doIt(1,2,3); claudio [1] http://java.sun.com/docs/books/jls/third_edition/html/ classes.html#8.4.1 -- Claudio Nieder, Talweg 6, CH-8610 Uster, Tel +4179 357 6743, www.claudio.ch |
|
#3
| |||
| |||
| On Aug 27, 6:32*am, Claudio Nieder <priv...@claudio.ch> wrote: > Hi, > > > I have been doing some practice exams and came across this funny '. . .' > > in the signatuer of a method: > > > int doIt(int... i) { > > return 4; } > > You can pass one or more integers. See chapter 8.4.1 of the Java Language > Specification[1] where it talks about "variable arity parameter". > > In the method body you treat i like it is declared an array of int. > > The difference to > > int doIt(int[] i) > > is that in this case whoever calls doIt has to pass an array, e.g.. call > it as doIt(new int[]{1,2,3}); while in the int... case you can just call > it as doIt(1,2,3); > > claudio > > [1]http://java.sun.com/docs/books/jls/third_edition/html/ > classes.html#8.4.1 > -- > Claudio Nieder, Talweg 6, CH-8610 Uster, Tel +4179 357 6743,www.claudio.ch Thanks for all the replys! |
|
#4
| |||
| |||
| On 27-8-2008 12:32, Claudio Nieder wrote: > Hi, > >> I have been doing some practice exams and came across this funny '. . .' >> in the signatuer of a method: >> >> int doIt(int... i) { >> return 4; } > > You can pass one or more integers. [...] Or zero. -- Regards, Roland |
|
#5
| |||
| |||
| Hi, >> You can pass one or more integers. [...] > > Or zero. Indeed. I didn't think of this. Thank you, claudio -- Claudio Nieder, Talweg 6, CH-8610 Uster, Tel +4179 357 6743, www.claudio.ch |
![]() |
| Thread Tools | |
| Display Modes | |
In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.