String的引用传递
一 引用传递的三个范例
范例一
package com.mtzsoft; /**
* 范例一
*
* @author Administrator
*
*/
public class Test1 { public static void main(String[] args) { Demo d1 = new Demo();
d1.setTemp(50); System.out.println("fun1调用之前temp=" + d1.getTemp());
fun1(d1);
System.out.println("fun1调用之后temp=" + d1.getTemp()); } public static void fun1(Demo d2) {
d2.setTemp(100);
}
}
class Demo{ private int temp=0; public int getTemp() {
return temp;
} public void setTemp(int temp) {
this.temp = temp;
} }
控制台打印结果:
调用fun1前值为50,调用后为100,方法所修改的值被保存下来了,那么我们进行内存分析如下:
* 引用传递(1)内存分析
*
* fun1(d1) 把d1的引用传递给d2 d2/d1共用内存空间
* -------------- ---------------
* | 栈 | | 堆 |
* --------------- ---------------
* | d1 | -----------------------------------------> | temp=50 |
* --------------- ↑ ---------------
* | d2 | --------------------
* --------------
* fun1(d1)执行后,d2断开连接
* -------------- ---------------
* | 栈 | | 堆 |
* --------------- ---------------
* | d1 | -----------------------------------------> | temp=100| d2/d1共用内存空间 d2修改temp的值
* --------------- ↑ ---------------
* | d2 | ----------x---------
* --------------
*/
范例二
package com.mtzsoft; /**
* 范例二
*
* @author Administrator
*
*/
public class Test2 { public static void main(String[] args) { String str = "hello"; System.out.println("fun2调用之前str=" + str);
fun2(str);
System.out.println("fun2调用之后str=" + str);
} public static void fun2(String s) {
s = "hello word";
}
}
控制台打印结果:
方法fun2调用前和调用后,str的值均为hello,那么String引用传递的内存分析:
/**
*
* 引用传递(2)内存分析 (String的不可改变的特性)
*
* fun2(str) 把str的引用传递给s
* -------------- ---------------
* | 栈 | | 堆 |
* --------------- ---------------
* | str | ----------------------------------------->| "hello" |
* --------------- ↑ ----------------
* | s | -------------------------
* --------------
*
* s="hello word" 开辟新内存空间
* -------------- -----------------
* | 栈 | | 堆 |
* --------------- ------------------
* | str | ----------------------------------------->| "hello" |
* --------------- ↑x 断开 ------------------
* | s | ------------------------- ---------------> | "hello word" |
* -------------- -------------------
*
* fun2(str)执行后
* -------------- -----------------
* | 栈 | | 堆 |
* --------------- ------------------
* | str | ----------------------------------------->| "hello" |
* -------------- ------------------
* | s | ------------------------- ---------------> | "hello word" |
* -------------- -------------------
*
*/
范例三
package com.mtzsoft; /**
* 范例三
*
* @author Administrator
*
*/
public class Test3 { public static void main(String[] args) { TestString t = new TestString();
t.setTemp("hello"); System.out.println("fun3调用之前temp=" + t.getTemp());
fun3(t);
System.out.println("fun3调用之后temp=" + t.getTemp());
} public static void fun3(TestString ts) { ts.setTemp("hello word");
}
} class TestString { private String temp = ""; public String getTemp() {
return temp;
} public void setTemp(String temp) {
this.temp = temp;
} }
控制台打印结果:
这里被修改的值保留下来了,与范例一相同,引用传递内存分析:
/**
*
* 引用传递(3)内存分析
*
* fun3(t) 把t的引用传递给t1
* -------------- ----------------------------
* | 栈 | | 堆 |
* --------------- -----------------------------
* | t | ----------------------------------------->| temp= "hello" |
* --------------- ↑ -----------------------------
* | ts | -------------------------
* ----------------
*
* t1.temp= "hello word";
* -------------- -------------------------------
* | 栈 | | 堆 |
* --------------- -------------------------------
* | t | -----------------------------------------> | temp= "hello word" |
* ---------------- ↑x 断开 -------------------------------
* | ts | ----------------------
* -----------------
*
*/
通过三道引用传递的分析:范例一与范例二是完全一样的,只是第二个范例体现了String类的内容不可改变的特性。
String的引用传递的更多相关文章
- Java中String是“引用”传递
如题,在java中这是一个典型的问题. 在stackoverflow上已经有很多相似的问题被提问,并且有很多不正确或不完整的答案.如果你不往深处想,这是一个很简单的问题.但如果深入思考,它却很让人迷惑 ...
- Java中String的 "引用" 传递
1.来看一段有趣但又让人困惑的代码片段 public static void main(String[] args){ String x = new String("ab"); c ...
- java值传递和引用传递的理解
java的基础数据类型有:(byte.short.int.long.float.double.char.boolean)八种 基础数据都是值传递,其他都是引用传递.但是引用传递要特别注意:String ...
- String作为方法参数传递 与 引用传递
String作为方法参数传递 String 和 StringBuffer的区别见这里: http://wenku.baidu.com/view/bb670f2abd64783e09122bcd.htm ...
- 如何引用传递String Boolean 等,并改变他们的值
如何引用传递String Boolean 等,并改变他们的值 采用list, 在存入位置改变list的值 如 list.add(true); list.remove(0); list.add(fals ...
- String是值传递还是引用传递
String是值传递还是引用传递 今天上班时,同事发现了一个比较有意思的问题.他把一个String类型的参数传入方法,并在方法内改变了引用的值. 然后他在方法外使用这个值,发现这个String还是之前 ...
- (摘录)String是值传递还是引用传递
String应该是一个封装类型,它应该是引用传递,是可以改变值的, 运行的结果应该是”cd”.我们实际运行一下看看, str=ab,这如何解释呢?难道String是基本类型?也说不通呀. 这就要从ja ...
- 对String值不可变的理解以及String类型的引用传递问题
今天复习java时,突然注意到了一句以前没有注意过的一句话,String 是final修饰的,其值是不可变的.当时看的一脸懵逼,String str = "abc"; str = ...
- 请问C#中string是值传递还是引用传递?
https://www.cnblogs.com/xiangniu/archive/2011/08/17/2143486.html 学了这么久,终于弄明白了... 是引用传递 但是string又有值传递 ...
随机推荐
- JsonUtils工具类
public class JsonUtils { public static void printTimeObject(Object obj, HttpServletResponse response ...
- Ukulele 那些花儿
- clover 显卡注入功能详细讲解
13 March 2014 GraphicsInjector功能源于变色龙,不过比变色龙更加灵活,定制性更加强大.Intel的显卡 GMA950, X3100, HD300, HD4000被证实可 ...
- proto_ops和proto区别
**proto_ops:**该结构体包含了某个特定的"protocol family"的一系列functions. 其包含的functions和struct proto很类似,但是 ...
- 气泡小角的css实现
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" dir="ltr"> ...
- Linux中断底半部机制
参考: Linux下半部处理之软中断 linux中断底半部机制 <深入理解Linux内核>软中断/tasklet/工作队列 软中断和tasklet介绍 详解操作系统中断 Linux内核:中 ...
- C/C++编程之内存管理
内存分配方式 内存分配方式一共有三种: (1)从静态存储区域分配: 内存在程序编译的时候就已经分配好,这块内存在程序的整个运行期间都存在,例如,全局变量,静态变量. (2)在栈上创建: 在执行函数时, ...
- PYDay3-初识python
Python 种类 c.j.iron.ruby等,主要有三类:cpython.xxxpython.pypy 种类繁多我们精通一种即可 编译流程: py代码->字节码->机器码->计算 ...
- oracle 安装 启动listener 建库相关
安装 参考 http://www.cnblogs.com/gaojun/archive/2012/11/22/2783257.html 几个问题: 1. 用户删除问题 p001:~ # userdel ...
- numpy hstack()
numpy.hstack(tup)[source] Stack arrays in sequence horizontally (column wise). Take a sequence of ar ...