• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Confused

 
Arun C. Giridharan
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This topic will explain it.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's even the question?
 
Arun C. Giridharan
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Wouter Oet.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49823
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Newton wrote:What's even the question?
And please put some hint of the question in the thread title.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1051
Eclipse IDE Firefox Browser
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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"
 
Vishal Kashyap
Ranch Hand
Posts: 73
BSD C++ Fedora
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Keep It Up Shanky..........Well Done.........
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vishal Kashyap wrote:Keep It Up Shanky..........Well Done.........
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic