aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question about Boxing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question about Boxing" Watch "Question about Boxing" New topic
Author

Question about Boxing

Patr�cia Oliveira
Greenhorn

Joined: Mar 29, 2006
Posts: 8
Someone can help me with the code below:

public class VarargsOverloading {

public void operation(String str) {
String signature = "(String)";
out.println(str + " => " + signature);
}

public void operation(String str, int m) {
String signature = "(String, int)";
out.println(str + " => " + signature);
}

public void operation(String str, int m, int n) {
String signature = "(String, int, int)";
out.println(str + " => " + signature);
}

public void operation(String str, Integer... data) {
String signature = "(String, Integer[])";
out.println(str + " => " + signature);
}

public void operation(String str, Number... data) {
String signature = "(String, Number[])";
out.println(str + " => " + signature);
}

public void operation(String str, Object... data) {
String signature = "(String, Object[])";
out.println(str + " => " + signature);
}

public static void main(String[] args) {
VarargsOverloading ref = new VarargsOverloading();
ref.operation("1. (String)");
ref.operation("2. (String, int)", 10);
ref.operation("3. (String, Integer)", new Integer(10));
ref.operation("4. (String, int, byte)", 10, (byte) 20);
ref.operation("5. (String, int, int)", 10, 20);
ref.operation("6. (String, int, long)", 10, 20L);
ref.operation("7. (String, int, int, int)", 10, 20, 30);
ref.operation("8. (String, int, double)", 10, 20.0);
ref.operation("9. (String, int, String)", 10, "what?");
ref.operation("10.(String, boolean)", false);
}
}

The answer is:
1. (String) => (String)
2. (String, int) => (String, int)
3. (String, Integer) => (String, int)
4. (String, int, byte) => (String, int, int)
5. (String, int, int) => (String, int, int)
6. (String, int, long) => (String, Number[])
7. (String, int, int, int) => (String, Integer[])
8. (String, int, double) => (String, Number[])
9. (String, int, String) => (String, Object[])
10.(String, boolean) => (String, Object[])

Why did the 3. (String, Integer) call the (String, int) intead of (String, Integer)?

Regards,

Pati


Pati
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
You do not have "public void operation(String str, Integer data)".
Why overloading a varargs method doesn't work for the primitive type and its object wrapper type?
[ August 08, 2006: Message edited by: wise owen ]
Edisandro Bessa
Ranch Hand

Joined: Jan 19, 2006
Posts: 584
In order to avoid "fork" on legacy Java code written in previous versions of SDK, primitive types arguments have high precedence over variable arguments (varargs).

And Wise is quite correct!!! There is no public void operation(String str, Integer data) signature
[ August 08, 2006: Message edited by: Edisandro Bessa ]

"If someone asks you to do something you don't know how to, don't tell I don't know, tell I can learn instead." - Myself
Patr�cia Oliveira
Greenhorn

Joined: Mar 29, 2006
Posts: 8
Yes, I do not have the public void operation(String str, Integer data) signature but I have the varargs public void operation(String str, Integer... data) signature. I tought it would be used.
vinod balaji
Ranch Hand

Joined: May 18, 2006
Posts: 84
primitive get preference over var args.
Var args is the last option.
Nilesh Patel
Ranch Hand

Joined: Feb 02, 2006
Posts: 91
Order Or Mathod Argumnent is ...

1. Boxing / var arg

First : Boxing
Secound:var arg

2. Widening / var arg

First : Widening
Secound:var arg


3.Boxing /Widening

First : Widening
Secound: Boxing


Means you can say that

First : Widening
Secound: Boxing
Third : var arg

You can use boxing before widening but not use widening befor boxing...


Nilesh Patel
SCJP 1.5 - 87%
 
jQuery in Action, 2nd edition
 
subject: Question about Boxing