This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Confused Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Confused" Watch "Confused" New topic
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

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: 37936
    
  22
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

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: 1051

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: 1051

Vishal Kashyap wrote:Keep It Up Shanky..........Well Done.........
 
 
subject: Confused
 
Similar Threads
how to access the elements of multidimension array using for each loop?
A Programming Interview Question
Question regarding the flow of execution with static blocks
problem...help me ..pls.
calling methods from main method