`

Java Pass By Value Example

 
阅读更多
package author.estone.java;

import java.util.Date;

public class PassByValue {

	public static void main(String[] args) {

		Date d1 = new Date("1 Apr 98");
		nextDateUpdate(d1);
		System.out.println("d1 after nextDay: " + d1);// date的值已被修改

		Date d2 = new Date("1 Apr 98");
		nextDateReplace(d2);
		System.out.println("d2 after nextDay: " + d2);// date的值没有被修改

	}

	private static void nextDateUpdate(Date arg) {
		arg.setDate(arg.getDate() + 1);
		System.out.println("arg in nextDay: " + arg);
	}

	private static void nextDateReplace(Date arg) {
		arg = new Date(arg.getYear(), arg.getMonth(), arg.getDate() + 1);
		System.out.println("arg in nextDay: " + arg);
	}

}

Console Output:

arg in nextDay: Thu Apr 02 00:00:00 CST 1998
d1 after nextDay: Thu Apr 02 00:00:00 CST 1998
arg in nextDay: Thu Apr 02 00:00:00 CST 1998
d2 after nextDay: Wed Apr 01 00:00:00 CST 1998

 

This code is from the book 《Refactoring》.

 

分享到:
评论

相关推荐

    Java邮件开发Fundamentals of the JavaMail API

    Note: After installing Sun's JavaMail implementation, you can find many example programs in the demo directory. Installing JavaMail 1.2 To use the JavaMail 1.2 API, download the JavaMail 1.2 ...

    JSP Simple Examples

    After going through this example you will be understand how you can calculate the factorial by using recursion in jsp. To make a program on factorial, firstly it must be clear what is recursion. ...

    利用python中xlrd模块批量读取excel多个工作表的单元格数据,并优化数据,绘制成表格.zip

    pass # 对非数字单元格保持原样 # 过滤掉空值 data = [row for row in data if any(row)] ``` 最后,如果要将数据绘制为表格,可以使用matplotlib的pyplot模块: ```python import matplotlib.pyplot as plt ...

    servlet2.4doc

    A filter configuration object used by a servlet container to pass information to a filter during initialization. flushBuffer() - Method in interface javax.servlet.ServletResponse Forces any content ...

    tomcat-5.5.23

    Directory used by the JVM for temporary files (java.io.tmpdir) <br>If you do not pass the "-Dcatalina.base=$CATALINA_BASE" argument to the startup command, $CATALINA_BASE will default to the ...

    Google C++ Style Guide(Google C++编程规范)高清PDF

    The goal of this guide is to manage this complexity by describing in detail the dos and don'ts of writing C++ code. These rules exist to keep the code base manageable while still allowing coders to ...

    C Programming

    - **The Program Example**: This is a step-by-step breakdown of the code, including comments and explanations for each line. - **#include **: Explanation of how the standard input/output library is ...

    python3.6.5参考手册 chm

    Bootstrapping pip By Default Documentation Changes PEP 446: Newly Created File Descriptors Are Non-Inheritable Improvements to Codec Handling PEP 451: A ModuleSpec Type for the Import System ...

    Python文档

    目前市场上的应用软件中有超过一半是面向网络的应用,如数据库服务器、游戏、Java Servlets 和 Applets、CGI 脚本等。数据通信不再是两台机器(本地网络或互联网)之间的奇技淫巧,而是日常生活中不可或缺的一部分。...

    测试培训教材

    For example, you can run a system cleanup test that will restart the machine on which an automated test failed. Alternatively, you can create a system test to retrieve information about a machine's ...

    超级全面的PHP面试题整理集合第1/2页

    ### 22、写出发贴数最多的十个人名字的SQL,利用下表:members(id,username,posts,pass,email)(2分) **SQL示例:** ```sql SELECT username FROM members ORDER BY posts DESC LIMIT 10; ``` ### 23.请说明php中...

Global site tag (gtag.js) - Google Analytics