Java. Overloading method
for example, i have this class:
public class Col {
static void test(int a)
{
System.out.println("int");
}
public static void main(String args[])
{
Col.test(12); //1
Col.test((byte)12); //2
Col.test((long)100); //3
}
}
and now me intresting how algoritm work this 开发者_运维技巧code. I think, that this steps:
1 line - all correct call method with int param, perfect.
2 line - call method with byte param...oooops. what do? Java try widening byte to int? Its true?
3 line call method with long param... again ooops. what do? convert long to int java can't, because loss of accuracy. its try? And in result - Exception.
Than I add this:
public static void test(Object a)
{
System.out.println("Object");
}
and if a call:
Col.test((long)100);
all correct, no Exception so, what the relation between primitive type long and Object?
Yes, there's an implicit conversion from byte
to int
, but no implicit conversion from long
to int
(because of the likelihood of losing information).
In the third case, you're using autoboxing which will convert a long
(primitive type) to a Long
(class type).
You can see that by changing the body of test
to:
public static void test(Object a)
{
System.out.println(a.getClass());
}
It will then print out class java.lang.Long
.
Your first example shows conversion of primitive types. The second shows boxing and unboxing, which is - in brief - a convenient conversion between primitive type (like long
) and their wrapper classes (java.lang.Long
in this case).
Overloading is implementing methods that have the same name but different parameters. Here we have two methods
static void test(int a){}
static void test(Object a){}
and call it with test((long) 100)
. The first method can't be called, because the JVM won't narrow a long
to an int
without explicit casting. But the JVM (Version 1.5+) can convert the long
value to a Long
(autoboxing) and test(Long.valueOf((long) 100))
is a good match for the second method.
This is because auto-boxing feature. Actually you have promoted the primitive to long and while calling test method automatically it is seaching for its equivalent type hence it is calling test(Object a).You can see like this Col.test(new Integer(12));this will also call test(Object a).Also you can refer this link Determining if an Object is of primitive type
public static void test(Object obj) {
if (obj instanceof Integer) {
System.out.println("Integer");
} else if (obj instanceof Double) {
System.out.println("Double");
} else if (obj instanceof Float) {
System.out.println("Float");
} else if (obj instanceof Long) {
System.out.println("Long");
}
}
All java primitives have corresponding boxed "types" that are actual classes. In you example, long has a corresponding class Long. This class extends from Object.
What you have experienced is boxing and unboxing.
It is a feature introduced in Java 5. Its called Autoboxing. In this a primitive type is converted to Object (in your case long to Long). See this link for details on Autoboxing.
精彩评论