Is Java pass by VALUE or pass by REFERENCE?

Today, I wrote a java class like below:
public class RefTest {

	public void changeString(String s){
		System.out.println("inner changeString() s="+s);
		s = "abc";
		
	}

	public static void main(String args[]){
		RefTest rf = new RefTest();
		String a = new String("a");
		System.out.println("before changeString() s="+a);
		rf.changeString(a);
		System.out.println("after changeString() s="+a);
	}
}
output:

before changeString() s=a
inner changeString() s=a
after changeString() s=a


The output seems that the method changeString() didn't change the String a, which was passed into the method. I searched the Internet and look for answers. My conclusion is as below:

Java has two kinds of variables, one is simple variables(e.g. int boolean etc.), another is references(e.g. String Character etc.).Let's talk about a question on how the two kinds of parameters passed into a method.

Simple variables:

let's look at a class:
public class RefTest {


	public void changeInt(int i){
		i = 5;
	}

	public static void main(String args[]){
		RefTest rf = new RefTest();
		
		
		int i = 2;
		rf.changeInt(i);
		System.out.println(i);
	}
}
Apparently, the output is 2.

after creating a simple variable like this:
int i = 2;
the value 5 is stored in stack,
 when you pass i into a method like this:
changeInt(i);
Java create another variable in stack whose value is 2,too. The newly created int will be collected after the method return.
So, even if the newly created int is changed into 5 inner method, the older int i, whose value is 2 is not changed.


 
Reference variables:

when creating a reference, two different memory is used: reference(similar to record memory of the Object) is stored in stack, Object is stored in heap.

the reference in Java is similar to the pointer in C, both of them are memory address. In this point,it is the same as simple variables.
Let's see the class at the beginning of the blog,

when the reference a is passed into method like this:
public void changeString(String s)
Java created another reference s, which point to the same memory address as a, both of them point to a String Object in heap.

when it comes to this line:
s = "abc";
the reference s is pointed to another String Object "abc".

when the method returns, s as the local variable will be free. the reference a certainly didn't change its value(the address of String"a").

Another example:
.
public class RefTest {

	public void changeString(StringBuffer s){
		System.out.println("inner changeString() s="+s);
		<pre name="code" class="java"><span style="white-space:pre">		</span>s.append("def");

}public static void main(String args[]){RefTest rf = new RefTest();StringBuffer a = new StringBuffer("abc");System.out.println("before changeString() s="+a);rf.changeString(a);System.out.println("after changeString() s="+a);}}
 
output:
before changeString() s=abc
inner changeString() s=abc
after changeString() s=abcdef


we know the local reference s and the reference a point to the same StringBuffer Object in heap.

when the line:
s.append("def");
is executed, the Object pointed by s is changed, which is just the same Object as a point.


As was discussed above, Java passes parameters by value.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值