2

Java 使用引用传递来修改值,这样做好吗?

 2 years ago
source link: https://www.v2ex.com/t/808054
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

V2EX  ›  程序员

Java 使用引用传递来修改值,这样做好吗?

  zzfer · 8 小时 39 分钟前 · 1280 次点击

今天在代码审核的时候看到这么一段代码,在 setProjectInfo 方法没有返回内容,但在方法体内给 list set 了值,想了下,是 Java 引用类型可以改变参数的值,这样写确实可行

	if (!CollectionUtils.isEmpty(list)){
            cmService.setProjectInfo(list);
        }
    	return list;

但如果是我写同样的需求,肯定是返回类型是 list<>,然后结果 return 给 list 。我不知道哪种写法好,想问问大家

	if (!CollectionUtils.isEmpty(list)){
            list = cmService.setProjectInfo(list);
        }
    	return list;

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK