java的基本数据类型的包装类是引用类型吗?
巴扎黑
巴扎黑 2017-04-18 10:37:36
0
2
563
巴扎黑
巴扎黑

Antworte allen(2)
刘奇

引用JVM规范中的一段,详情参见

For every parameter declared in a method declaration, a new parameter variable is created each time that method is invoked. The new variable is initialized with the corresponding argument value from the method invocation.

规范中说每次方法调用,方法中的参数都会初始化。

上面test方法的index就是初始化了一个Integer类型的变量然后指向传入的i。后面的index = new Integer(20); 只是改变了index变量的指向,原来i的指向并没有改变。

Ty80

java 中方法用到的都是值传递,即使是引用,传递的也是原来引用的一个拷贝值

方法只能让参数引用中的属性改变,而不能改变参数的引用地址

而且你可以看Integer的源码:

它包装的值value是被final修饰的,也就是只能被赋值一次,赋值后值不再改变。

另外你可以试一下别的引用类型,看看能不能被改变,比如String

Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!