• 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

Chap 1 ex 6 K&B (varargs)

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys,

my issues seem to be ignored all the time but I am anyway going to write again hoping to get some response.

In the K&B book, chapter 1, exercise 6 it is asked if, considering the following calls

doStuff(1);
doStuff(1,2);

which possible method signatures for doStuff are possible. In the list of options there is also

static void doStuff(int[] doArgs)

That is not considered a valid response but I tested it and it works.

what do you think?

Thanks,

Mohan
 
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mohan,

Are you sure the code compiles ? I think you are using a previous valid version of .class file.

I tried the following code and it doesn't compile.



I get the following error at both lines 1 and 2, respectively

doStuff(int[]) in Voop cannot be applied to (int)

doStuff(int[]) in Voop cannot be applied to (int,int)
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What does your test program look like. This code resulted in a compiler error on my machine.


the error was:

[ March 24, 2006: Message edited by: Garrett Rowe ]
 
Mohan Dinesh Ganti
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys,

actually you are right, I don't know how but I got confused and tested the opposite scenario



which of course worked :-)

Mohan
 
Mohan Dinesh Ganti
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Of course it worked with static added to the test method ;-)

Mohan
 
Edisandro Bessa
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mohan,

Recently I got a question from Whizlabs simulator something like that.

Please let me use your code as example ok ?



What is the result of preceeding code ?

a)int[]
b)int ...
c)compilation fails
d)no output at runtime
e)runtime error

At a first look I tended to choose (a) once I am sending an int array and there's a corresponding method to call.

But, for my surprising the code doesn't compile.

To compiler both test method definitions are the same. Very interesting.
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
but it is suppose to compile since the jvm refers to all methods be4it can consider varargs so I guess it is suppose to print int[]
 
Mohan Dinesh Ganti
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, it's interesting to know.. you never know what the exam will ask ;-)

Thansk for sharing your experiment,

Mohan
 
Edisandro Bessa
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
After practice a few examples about varargs I could figure out the following rules :

Arrays can be passed to methods which signature have either array types or varargs types.

Code Sample :

int [] a = {1,2,3,4,5}

Method with both signatures int[] or int ... are able to receive this array as argument.

Primitive types can be passed only to methods which signature is fixed or primitive variable-length

Code Sample

doStuff(1,2,3);

Can be passed to either methods with signature int x, int y, int z or int ...

Methods with int[] signature are not able to receive primitive varargs.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic