栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何通过引用正确传递Integer类?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何通过引用正确传递Integer类?

有两个问题:

  1. 整数是通过值而不是通过引用传递的。在方法内部更改引用不会反映到调用方法中的传入引用中。
  2. 整数是不可变的。没有像这样的方法
    Integer#set(i)
    。否则,您可以利用它。

要使其正常工作,您需要重新分配该

inc()
方法的返回值。

integer = inc(integer);

要了解有关按值传递的更多信息,这是另一个示例:

public static void main(String... args) {    String[] strings = new String[] { "foo", "bar" };    changeReference(strings);    System.out.println(Arrays.toString(strings)); // still [foo, bar]    changevalue(strings);    System.out.println(Arrays.toString(strings)); // [foo, foo]}public static void changeReference(String[] strings) {    strings = new String[] { "foo", "foo" };}public static void changevalue(String[] strings) {    strings[1] = "foo";}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/381730.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号