File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Chap 1 ex 6 K&B (varargs) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Chap 1 ex 6 K&B (varargs)" Watch "Chap 1 ex 6 K&B (varargs)" New topic
Author

Chap 1 ex 6 K&B (varargs)

Mohan Dinesh Ganti
Greenhorn

Joined: Mar 23, 2006
Posts: 11
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
Edisandro Bessa
Ranch Hand

Joined: Jan 19, 2006
Posts: 584
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)


"If someone asks you to do something you don't know how to, don't tell I don't know, tell I can learn instead." - Myself
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
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 ]

Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Mohan Dinesh Ganti
Greenhorn

Joined: Mar 23, 2006
Posts: 11
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

Joined: Mar 23, 2006
Posts: 11
Of course it worked with static added to the test method ;-)

Mohan
Edisandro Bessa
Ranch Hand

Joined: Jan 19, 2006
Posts: 584
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.
mambe nanje
Ranch Hand

Joined: Feb 22, 2006
Posts: 31
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[]


Da Clone in programming world
Mohan Dinesh Ganti
Greenhorn

Joined: Mar 23, 2006
Posts: 11
Yes, it's interesting to know.. you never know what the exam will ask ;-)

Thansk for sharing your experiment,

Mohan
Edisandro Bessa
Ranch Hand

Joined: Jan 19, 2006
Posts: 584
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.
 
jQuery in Action, 2nd edition
 
subject: Chap 1 ex 6 K&B (varargs)