`
george.gu
  • 浏览: 73510 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Java is Pass-by-Value or Pass-by-Reference?

 
阅读更多

What's saved in Object Reference?

We can treat the Java Object will be managed in two parts:

 

  1. Obejct definition and its reference to Memory Heap which contains object's attribute and data contents.
  2. Data contents in memory heap which is known by Object by reference.

The following Junit test code prove this understanding.

 

	class TempString {
		private String temp;

		public TempString(String str) {
			this.temp = str;
		}

		public String getTemp() {
			return temp;
		}

		public void setTemp(String temp) {
			this.temp = temp;
		}
	}

	@Test
	public void testObjectReference() {
		TempString obj = new TempString("test");
		TempString objb = obj;

		obj = null;
		TempString objc = obj;
		assertNull(obj);
		assertNull(objc);
		assertNotNull(objb);
		assertEquals("test", objb.getTemp());
	}
 

Set Object to be NULL: Object=null

We can use the "Object=null" to release memory. But keep in mind, it is not "=null" operation itself make memory released. Garbage Collector will clean and release memory heap later if it found there is no any reference to the data (in memory heap).

 

List keep the Object reference but not object itself

From below test, we can know that: Collect keep the object reference to its memory heap.

 

	@Test
	public void testListKeepOriginalAfterSetObjectTobeNull() {
		TempString obj = new TempString("test");
		List<TempString> list = new ArrayList<TempString>();
		list.add(obj);

		obj = null;

		assertNull(obj);
		assertEquals("test", list.get(0).getTemp());

		List<String> list2 = new ArrayList<String>();
		String a = new String("StringObject");
		String b = "testPrimitive";
		list2.add(a);
		list2.add(b);
		a = null;
		b = null;
		assertEquals("StringObject", list2.get(0));
		assertEquals("testPrimitive", list2.get(1));

		List<Integer> list3 = new ArrayList<Integer>();
		Integer intA = new Integer(1);
		Integer intB = 5;
		list3.add(intA);
		list3.add(intB);
		intA = null;
		intB = null;
		assertNotNull(list3.get(0));
		assertEquals(1, list3.get(0).intValue());
	}

 

Java is Pass-by-Value or Pass-by-Reference?

To be Updated. Here I list the doc I found from Internet:

 

 

Clone your Java Object

To be Updated. Here I list the doc I found from Internet: 

 

分享到:
评论

相关推荐

    条款20: 宁以pass-by-reference-to-const 替换 pass-by-value

    1、为什么要宁以pass-by-reference-to-const 替换 pass-by-value 效率方面 缺省情况下,C++以by value 方式传递对象至(或来自)函数。 除非你另外指定,否则函数参数都是以实际实参的副本为初值,而调用段所获得的...

    EC.zip_eC_inheritance

    Between pass-by-value and pass-by-reference? It is important to get these decisions right at the outset, because an incorrect choice may not become apparent until much later in the development ...

    js-pass-by-value-vs-pass-by-reference-arrays:测试Java中数组的按值传递与按引用传递

    在Javascript中测试数组的按值传递与按引用传递。 解释 我最初查看Javascript是按值传递还是引用传递,发现除对象之外,它按值传递。 我创建了一个测试环境,该环境将创建一个大的整数数组并将该数组多次传递给函数...

    02-pass-w-d-simoneLentini

    在Java编程中,"passing with default" 或 "pass by reference vs pass by value" 是一个常见的讨论话题。Java中所有的参数传递都是按值传递,这意味着当你将一个变量传递给方法时,方法接收的是该变量当前值的一个...

    PC-lint 9.0 -- Gimpel.Software.PC-lint.9.0a

    You have permission to print out the Reference Manual (or other related documentation) in whole or in part in support of the use of this software. ------ Support for Microsoft through Visual ...

    delphi传址与传参的区别

    在Delphi编程语言中,函数调用时参数的传递方式主要分为两种:传值(Pass-by-value)和传址(Pass-by-reference)。这两种传递方式在实际编程中有着本质的区别,理解它们之间的差异对于编写高效、安全的代码至关重要...

    世界五百强java面试题下载

    - Stack Overflow上的相关讨论:https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value 通过以上分析和扩展阅读,希望能帮助大家更好地理解和掌握Java中对象传递的关键概念。

    Java编程那些事儿55—方法重载和参数传递

    Java中的参数传递有两种主要的方式:值传递(Pass-by-value)和引用传递(Pass-by-reference)。虽然Java主要是基于值传递的,但在处理对象时,实际上传递的是指向对象的引用,因此在某些情况下也会被称为引用传递。...

    servlet2.4doc

    Returns the servlet container attribute with the given name, or null if there is no attribute by that name. getAttribute(String) - Method in class javax.servlet.ServletRequestWrapper The default ...

    Effective C++(第三版)

    条款20:宁以pass-by-reference-to-const替换pass-by-value prefer pass-by-reference-to-const to pass-by-value. 条款21:必须返回对象时,别妄想返回其reference don't try to return a reference when you must ...

    java参数的传递与返回值

    参数的传递方式主要有两种:值传递(Pass-by-value)和引用传递(Pass-by-reference)。Java主要采用值传递的方式。 ##### 1. 值传递 当我们将基本数据类型作为参数传递给方法时,实际上是传递了该数据的值的一个...

    Bochs - The cross platform IA-32 (x86) emulator

    The default stepping value is 3. - Added ability to disable MONITOR/MWAIT support through .bochsrc CPUID option. The option is available only if compiled with --enable-monitor-mwait configure ...

    Java邮件开发Fundamentals of the JavaMail API

    The Simple Mail Transfer Protocol (SMTP) is defined by RFC 821 . It defines the mechanism for delivery of e-mail. In the context of the JavaMail API, your JavaMail-based program will communicate ...

    java笔记整理

    - **传引用与传值的区别**:Java不支持传引用(`pass-by-reference`),这与C++等语言有所不同。传引用是指传递变量的地址,而不是其值。 #### 四、方法重载与构造方法重载 - **方法重载概述**:方法重载(Overloading...

    BURNINTEST--硬件检测工具

    This will error faulty NICs that are not detected by the BurnInTest auto NIC detection mechanism. - Minor change to the 2D memory test when run with the 3D test (multiple large windows) and the ...

    《HEAD FIRST JAVA》笔记

    - Java使用传值调用(pass-by-value),即传递给方法的是实参的一个副本。如果是引用类型的参数,则传递的是引用的副本。 - **封装(encapsulation)**: - 使用setter和getter方法可以增强对象的安全性。通过将实例...

    程序员面试刷题的书哪个好-effective-cpp-note:EffectiveC++、MoreEffectiveC++和Effective

    因此当这四个子语言相互切换的时候,可以更多地考虑高效编程,例如pass-by-value和pass-by-reference在不同语言中效率不同 总结: C++高效编程守则视状况而变化,取决于使用哪个子语言 2. 尽量以const, enum, inline...

    php.ini-development

    directive because it is not set or is mistyped, a default value will be used. ; The value can be a string, a number, a PHP constant (e.g. E_ALL or M_PI), one ; of the INI constants (On, Off, True, ...

Global site tag (gtag.js) - Google Analytics