博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Overloaded Methods and AutoboxingUnboxing
阅读量:6114 次
发布时间:2019-06-21

本文共 5073 字,大约阅读时间需要 16 分钟。

################## key points extracted ###########################

The rule for a method invocation that uses autoboxing/unboxing follows a two-step process.

If the actual argument being passed is a primitive type (as in test(10)),

Try to find a method with the primitive type argument. If there is no exact match, try widening the primitive type to find a match.
If the previous step fails, box the primitive type and try to find a match.
If the actual argument being passed is a reference type (as in test(new Integer(101)),
Try to find a method with the reference type argument. If there is match, call that method. In this case, a match does not have to be exact. It should follow the subtype and super type assignment rule.
If the previous step fails, unbox the reference type to the corresponding primitive type and try to find an exact match, or widen the primitive type and find a match.

 

#####################################################################

 

Overloaded Methods and Autoboxing/Unboxing

You have a few surprises when you call an overloaded method and want to rely on autoboxing/unboxing feature. Suppose you have two methods in a class.

public void test(Integer iObject) {

System.out.println("Integer=" + iObject);
}
public void test(int iValue) {
System.out.println("int=" + iValue);
}

Suppose you make two calls to the test() method.

test(101);

test(new Integer(101));

Which of the following will be the output?

int=101

Integer=101

or

Integer=101

int=101

The rule for a method invocation that uses autoboxing/unboxing follows a two-step process.

If the actual argument being passed is a primitive type (as in test(10)),

Try to find a method with the primitive type argument. If there is no exact match, try widening the primitive type to find a match.
If the previous step fails, box the primitive type and try to find a match.
If the actual argument being passed is a reference type (as in test(new Integer(101)),
Try to find a method with the reference type argument. If there is match, call that method. In this case, a match does not have to be exact. It should follow the subtype and super type assignment rule.
If the previous step fails, unbox the reference type to the corresponding primitive type and try to find an exact match, or widen the primitive type and find a match.

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

Are you surprised by looking at the above output? Apply the rules that I have listed and you will find that the above output followed those rules. The call to test(101) is clear because it widens 101 from int to long and executes the test(long) method. To call test(new Integer(101)), it looks for a method test(Integer) and it does not find one. Note that a reference type is never widened. That is, an Integer is never widened to Long. Therefore, it unboxes the Integer to int and looks for test(int) method, which it does not find. Now, it widens the int and finds test(long) and executes it.

I have one more surprise. Consider the following two test() methods:

public void test(Long lObject) {

System.out.println("Long=" + lObject);
}
public void test(Object obj) {
System.out.println("Object=" + obj);
}

What will be printed when you execute the following code?

test(101);

test(new Integer(101));

This time, you will get the following output:

Object=101

Object=101

Does it make sense? Not really. Here is the explanation. When it calls test(101), it has to box int to an Integer, because there is no match for test(int), even after widening the int value. So, test(101) becomes test(Integer.valueOf(101)). Now it does not find any test(Integer) either. Note that Integer is a reference type and it inherits the Number class, which in turn inherits the Object class. Therefore, an Integer is always an Object, and Java allows you to assign an object of subtype (Integer) to a variable of supertype (Object). This is the reason that the test(Object) is called in this case. The second call, test(new Integer(101)), works the same way. It tries for test(Integer) method. When it does not find it, the next match for it is test(Object) based on the subtype and supertype assignment rule for reference types.

转载于:https://www.cnblogs.com/glf2046/p/4885422.html

你可能感兴趣的文章
Centos 6.x 升级openssh版本
查看>>
公式推♂倒题
查看>>
vue实现点击展开,点击收起
查看>>
如何使frame能居中显示
查看>>
第k小数
查看>>
构建之法阅读笔记三
查看>>
Python/PHP 远程文件/图片 下载
查看>>
【原创】一文彻底搞懂安卓WebView白名单校验
查看>>
写给对前途迷茫的朋友:五句话定会改变你的人生
查看>>
并行程序设计学习心得1——并行计算机存储
查看>>
JAVA入门到精通-第86讲-半双工/全双工
查看>>
bulk
查看>>
js document.activeElement 获得焦点的元素
查看>>
C++ 迭代器运算
查看>>
【支持iOS11】UITableView左滑删除自定义 - 实现多选项并使用自定义图片
查看>>
day6-if,while,for的快速掌握
查看>>
JavaWeb学习笔记(十四)--JSP语法
查看>>
【算法笔记】多线程斐波那契数列
查看>>
java8函数式编程实例
查看>>
jqgrid滚动条宽度/列显示不全问题
查看>>