• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question about Boxing

 
Patr�cia Oliveira
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
wise owen
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 584
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Patr�cia Oliveira
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
primitive get preference over var args.
Var args is the last option.
 
Nilesh Patel
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic