aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Clarification with Overloading and varargs usage Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Clarification with Overloading and varargs usage" Watch "Clarification with Overloading and varargs usage" New topic
Author

Clarification with Overloading and varargs usage

Madhu Pavuluri
Greenhorn

Joined: Jul 23, 2008
Posts: 4
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

Joined: Jun 04, 2007
Posts: 331
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


Blog
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1



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 !)


[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Raphael Rabadan
Ranch Hand

Joined: Jul 05, 2008
Posts: 141
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.


SCJP Java 6 (98%) - Story, SCJA (88%) - Story
Justin Smith
Greenhorn

Joined: Jul 24, 2008
Posts: 19
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

Joined: Dec 11, 2004
Posts: 13410

"Madhu PK",
Please check your private messages regarding an important administrative matter.
-Ben


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Raphael Rabadan
Ranch Hand

Joined: Jul 05, 2008
Posts: 141
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

Joined: Jul 23, 2008
Posts: 4
Thanks for all the replies. This clarifies my question.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

"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


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Lino Larios
Greenhorn

Joined: Jun 27, 2008
Posts: 28
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Clarification with Overloading and varargs usage