• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Clarification with Overloading and varargs usage

 
Madhu Pavuluri
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone help me by throwing some light on the below code examples

I am refering to 2 programs related to Overloading and use of varargs.
The below code throws an compile error since 2 methods have same syntax with concept of boxing in. I am ok with this.

class etattva105 {
public static void check(Double d1, Double ... d2) {
System.out.println("Double , Var-Args");
}
public static void check(Double d1, Double d2, Double ... d3) {
System.out.println("Double , Double , Var-args");
}
public static void check(double d1, Double d2, Double ... d3) {
System.out.println("double , Double , Var-args");
}
public static void main(String [] args) {
check(12.3, 21.9, 37.7); // line 1

Here comes second prg.
The below code compiles fine and displays "Integer,Integer is called".
Similar to above prg, I expected this prg also to throw a compile error, but it goes fine. Can some one explain?

public class etattva10 {
public static void main(String[] args) {
int i = 10;
int k = 20;
method(i,k);
}
static void method(Integer... i){
System.out.println("Integer varargs is called");
}
static void method(Integer i,Integer j){
System.out.println("Integer,Integer is called");
}
static void method(int... i){
System.out.println("int varargs is called");
}
}
 
Vishwanath Krishnamurthi
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

In the second code, the compiler tries to see the most specific match out of the the three, and it doesnt have any problems in making the choice....(No ambiguities)

Integer, Integer

is more specific than

int...

which in turn is more specific than

Integer...


HTH,

Vishwa
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Just keep in mind, Integer is more class and like to take refernce of Integer type ( though it can take int , by boxing) and int is simple classic primitive data type..

so dont get confused !!

and plese use code tag to sorrund the code snippet !, it looks [pretty in it (and easier to read !)
 
Raphael Rabadan
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Madhu PK,


Remember, the order is:

1st. No Boxing or Var-args -> Primitive to Primitive / Wrapper to Wrapper
2nd. Autoboxing -> Primitive to Wrapper / Wrapper to Primitive
3rd. Var-args

Since there you had a option with only Autoboxing to the call, it should work ok, the compiler wont even look at the var-args.

When you use var-args, wont matter if its boxing or no, the priority is just Var-args, if you have two that matters.. Compile time error on calling it.
 
Justin Smith
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Raphael,

Remember, the order is:

1st. No Boxing or Var-args -> Primitive to Primitive / Wrapper to Wrapper
2nd. Autoboxing -> Primitive to Wrapper / Wrapper to Primitive
3rd. Var-args

In the 1st are you trying to say??

No Boxing or widening -> Primitive to primitive (int to long)
2nd and 3rd are correct...

Remember the order

Widening -> Boxing -> Var-args

Var args get the least priority.....
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Madhu PK",
Please check your private messages regarding an important administrative matter.
-Ben
 
Raphael Rabadan
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Justin Smith:
Hi Raphael,

Remember, the order is:

1st. No Boxing or Var-args -> Primitive to Primitive / Wrapper to Wrapper
2nd. Autoboxing -> Primitive to Wrapper / Wrapper to Primitive
3rd. Var-args

In the 1st are you trying to say??

No Boxing or widening -> Primitive to primitive (int to long)
2nd and 3rd are correct...

Remember the order

Widening -> Boxing -> Var-args

Var args get the least priority.....


In the 1st I meant it doesn't use Boxing or Var-args(doesnt use both), in other words, you'll pass a primitive, to primite, or an Wrapper to an Wrapper.
 
Madhu Pavuluri
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all the replies. This clarifies my question.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65123
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Madhu PK", Ben's request via PM that you change your display name to adhere to JavaRanch standards was not a suggestion. Valid display names are mandatory for participation on the Ranch. Please change your display name as instructed prior to your next post.

Be aware that accounts with invalid display names are disabled.

bear
JavaRanch Sheriff
 
Lino Larios
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i remember that var agrs are always loosers, and you can box and then widen


class Test {
static void box(Object o) {
Integer i2 = (Integer) o; //
System.out.println(i2);
}
public static void main(String [] args) {
int i = 5;
box(b);
}
}
[ July 28, 2008: Message edited by: Lino Larios ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic