Let's say you have several methods with the same name but different parameters(overloaded methods).
You pass a "primitive wrapper object" OR a primitive.
What is chosen ? and Why ?
Diagram :
A) a Short
can become a Short or an Object (is an Object)
WILL NOT BECOME an Integer, a Long, a Float, a Double
Short is Short OR Object STOP
OR can become a short (a primitive)
look below
B) a short (primitive)
can become (what is short compatible) a short, a int, a long, a float, a double (I mean without a cast) STOP
OR
a Short (primitive wrapper)
Look now the way a Short is evaluated above.
I have to add a rule, var-arg parameters are ALWAYS - ALWAYS - ALWAYS :-) chosen LAST ! Look at example AB2 (AutoBoxing2).
I like the style of Kathy :-)
Examples
Please find the correct answers :
package autoboxing;
public class AB1 {
static String method1(Short param) {
return "Short";
}
static String method1(short param) {
return "short";
}
public static void main(String[] args) {
Short x = -32768;
System.out.println(method1(x));
}
}
package autoboxing;
public class AB2 {
static String method1(Short... param) {
return "Short...";
}
static String method1(Object param) {
return "Object";
}
public static void main(String[] args) {
Short x = -32768;
System.out.println(method1(x));
}
}
package autoboxing;
public class AB31 {
static String method1(short param) {
return "short";
}
static String method1(Object param) {
return "Object";
}
public static void main(String[] args) {
Short x = -32768;
System.out.println(method1(x));
}
}
package autoboxing;
public class AB32 {
static String method1(short param) {
return "short";
}
static String method1(double param) {
return "double";
}
public static void main(String[] args) {
Short x = -32768;
System.out.println(method1(x));
}
}
package autoboxing;
public class AB33 {
static String method1(double param) {
return "double";
}
static String method1(int param) {
return "int";
}
public static void main(String[] args) {
Short x = -32768;
System.out.println(method1(x));
}
}
package autoboxing;
public class AB4 {
static String method1(short param) {
return "short";
}
static String method1(Short param) {
return "Short";
}
public static void main(String[] args) {
short x = -32768;
System.out.println(method1(x));
}
}
package autoboxing;
public class AB5 {
static String method1(double param) {
return "double";
}
static String method1(Short param) {
return "Short";
}
public static void main(String[] args) {
short x = -32768;
System.out.println(method1(x));
}
}
package autoboxing;
public class AB6 {
static String method1(Double param) {
return "Double";
}
public static void main(String[] args) {
short x = -32768;
System.out.println(method1(x));
}
}
package autoboxing;
public class AB7 {
static String method1(Short param) {
return "Short";
}
static String method1(Object param) {
return "Object";
}
public static void main(String[] args) {
short x = -32768;
System.out.println(method1(x));
}
}
package autoboxing;
public class AB8 {
static String method1(Integer param) {
return "Integer";
}
static String method1(Object param) {
return "Object";
}
public static void main(String[] args) {
short x = -32768;
System.out.println(method1(x));
}
}
Answers :
AB1:Short,AB2:Object,AB31:Object,AB32:short,AB33:int,AB4:short,AB5:double,AB6:does not compile,AB7:Short,AB8:Object
*** ATTENTION ***
ReplyDeletea Short can also become a Number !!!
Short--->Number
I forgot this case...
I will update this page very soon...
When are you planning to update Short -> Number case?
ReplyDeleteSorry I was rather busy the last 2 years... Sorry again !
DeleteI will read all the blog and based on the experience I have gained the last 2 years I will make something better...
Hello,
ReplyDeleteyou should correct AB1, they both print "Short"
It is now corrected. Thanks a lot !
ReplyDelete