Author
Confused
Arun C. Giridharan
Ranch Hand
Joined: Jul 11, 2010
Posts: 96
javachamp Q1
1 Long x, Long y
2 int... x
3 Compilation error
4 An exception is thrown at run time
The Answer was static void call(int... x) .....how is that possible? Can anyone Explain me!
Wouter Oet
Saloon Keeper
Joined: Oct 25, 2008
Posts: 2700
This topic will explain it.
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
David Newton
Author
Rancher
Joined: Sep 29, 2008
Posts: 12617
posted Aug 15, 2010 10:59:11
0
What's even the question?
Arun C. Giridharan
Ranch Hand
Joined: Jul 11, 2010
Posts: 96
Thank you Wouter Oet.
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32641
David Newton wrote: What's even the question?
And please put some hint of the question in the thread title .
David Newton
Author
Rancher
Joined: Sep 29, 2008
Posts: 12617
posted Aug 16, 2010 05:37:42
0
Campbell Ritchie wrote: And please put some hint of the question in the
thread title .
Good point.
Technically every single post I've ever made in my life could be titled "Confused."
Shanky Sohar
Ranch Hand
Joined: Mar 17, 2010
Posts: 1046
public class Tester {
static void call(Long x, Long y) {
System.out.print("Long x, Long y");
}
static void call(int... x) {
System.out.print("int... x");
}
public static void main(
String [] args) {
int val = 3;
call(val, val);
}
}
compiler choose..
1.matching argument
2.widening
3.autoboxing.
4.Variable argument.
if you remove
then compiler says compile time error.on call(val,val);
because it is not legal to first autobox(int to Long) and then widened it to long.
but if you change
with this
then output will be "long x, long y"
because it is perfectly legal to widening.
So,In your code
compiler doesnot even see the code other call(Long x,Long y) because it is finding other one to match which is
that why output is "int....x"
SCJP6.0,My blog Ranchers from Delhi
Vishal Kashyap
Ranch Hand
Joined: Aug 07, 2010
Posts: 73
Keep It Up Shanky..........Well Done.........
MCSA 2003 | Preparing For OCPJP/SCJP6
Shanky Sohar
Ranch Hand
Joined: Mar 17, 2010
Posts: 1046
Vishal Kashyap wrote: Keep It Up Shanky..........Well Done.........
I agree. Here's the link: jrebel
subject: Confused