Java Reference
In-Depth Information
If you apply these rules to the above snippet of code, it will print
int=101
Integer=101
Suppose you have two
test()
methods.
public void test(Integer iObject) {
System.out.println("Integer=" + iObject);
}
public void test(long iValue) {
System.out.println("long=" + iValue);
}
What will be printed if you use the following code?
test(101);
test(new Integer(101));
It will print
long=101
Integer=101
The first call of
test(101)
will try to find an exact match for an
int
argument. It does not find a method
test(int)
, so it widens the
int
data type, finds a match
test(long)
, and calls this method.
Suppose you have two
test()
methods.
public void test(Long lObject) {
System.out.println("Long=" + lObject);
}
public void test(long lValue) {
System.out.println("long=" + lValue);
}
What will be printed if you execute the following code?
test(101);
test(new Integer(101));
It will print
long=101
long=101