Question on syntax

This is a discussion on Question on syntax within the Java forums in Programming Languages category; I have been doing some practice exams and came across this funny '. . .' in the signatuer of a method: int doIt(int... i) { return 4; }...

Go Back   Application Development Forum > Programming Languages > Java

Object Mix

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 08-26-2008, 10:12 PM
maxnesler@gmail.com
Guest
 
Default Question on syntax

I have been doing some practice exams and came across this funny
'. . .' in the signatuer of a method:

int doIt(int... i) {
return 4; }
Reply With Quote
  #2  
Old 08-27-2008, 06:32 AM
Claudio Nieder
Guest
 
Default Re: Question on syntax

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
Reply With Quote
  #3  
Old 08-27-2008, 02:13 PM
maxnesler@gmail.com
Guest
 
Default Re: Question on syntax

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!
Reply With Quote
  #4  
Old 08-27-2008, 05:14 PM
Roland de Ruiter
Guest
 
Default Re: Question on syntax

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
Reply With Quote
  #5  
Old 08-28-2008, 07:51 AM
Claudio Nieder
Guest
 
Default Re: Question on syntax

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
Reply With Quote
Reply


Thread Tools
Display Modes


All times are GMT -5. The time now is 06:54 PM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
vB Ad Management by =RedTyger=

In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.