Java数据传递实验
本文来自http://blog.csdn.net/liuxian13183/ ,引用必须注明出处!
在开发过程中,我们经常会遇到对象传递的问题,有时仅仅传递数据,有时却要实现数据同步;这时,就要分清两者间的区别。
public class DelegentDemo {
public void changStr(String str) {
str = "cde";
} public void addStr(String str) {
String bString = new String("cde");
str += bString;
}
public void contactStr(String str) {
String bString = new String("cde");
str.concat(bString);
} public void changeInt(int a) {
a = 2;
} public void addInt(int a) {
a += 2;
a++;
} /**
* @param stringBuffer
*/
private void changStringBuffer(StringBuffer stringBuffer) {
// TODO Auto-generated method stub
stringBuffer.append("db");
} /**
* @param list
*/
private void newList(List<String> list) {
// TODO Auto-generated method stub
list = new ArrayList<String>();
} /**
* @param list
*/
private void changeList(List<String> list) {
// TODO Auto-generated method stub
list.add("cdf");
} public static void main(String[] args) {
DelegentDemo delegent = new DelegentDemo();
String str1 = new String("abc");
delegent.changStr(str1);
System.out.println("abc传递常量重新赋值cde后:" + str1);
delegent.addStr(str1);
System.out.println("abc传递常量加值cde后:" + str1);
delegent.contactStr(str1);
System.out.println("abc传递常量contact值cde后:" + str1);
int a = 3;
delegent.changeInt(a);
System.out.println("3传递常量重新赋值2后:" + a);
delegent.addInt(a);
System.out.println("3传递常量加值2后:" + a); List<String> list = new ArrayList<String>();
list.add("abc");
delegent.changeList(list);
System.out.println("List size=1对象传递加值cdf后的大小:" + list.size());
delegent.newList(list);
System.out.println("List size=1 对象置空后的大小:" + list.size()); StringBuffer stringBuffer = new StringBuffer("adb");
delegent.changStringBuffer(stringBuffer);
System.out.println("adb传递加值db后"+stringBuffer.toString());
}
}结果如下:
abc传递常量重新赋值cde后:abc
abc传递常量加值cde后:abc
abc传递常量contact值cde后:abc
3传递常量重新赋值2后:3
3传递常量加值2后:3
List size=1对象传递加值cdf后的大小:2
List size=1 对象置空后的大小:2
adb传递加值db后adbdb结论:
1、传递的String常量值改变(包含赋值和加值)后,等于产生一个新的变量,此引用指向新变量,而原引用仍然指向原变量,所以打印原引用,当然值不会发生改变。
例:
public String trim() {
int start = offset, last = offset + count - 1;
int end = last;
while ((start <= end) && (value[start] <= ' ')) {
start++;
}
while ((end >= start) && (value[end] <= ' ')) {
end--;
}
if (start == offset && end == last) {
return this;
}
return new String(start, end - start + 1, value);
}不论trim还是contact方法,均如此,这时String被当成基本数据常量来处理
常量都存储在栈,Int也是同样原理。
至于容器和对象,如List和StringBuffer,它们是存储在堆里面的,传递引用过去,操作后会改变值。
例:
/**
* Adds the specified object at the end of this {@code ArrayList}.
*
* @param object
* the object to add.
* @return always true
*/
@Override public boolean add(E object) {
Object[] a = array;
int s = size;
if (s == a.length) {
Object[] newArray = new Object[s +
(s < (MIN_CAPACITY_INCREMENT / 2) ?
MIN_CAPACITY_INCREMENT : s >> 1)];
System.arraycopy(a, 0, newArray, 0, s);
array = a = newArray;
}
a[s] = object;
size = s + 1;
modCount++;
return true;
}size被加1,
/**
* Returns the number of elements in this {@code ArrayList}.
*
* @return the number of elements in this {@code ArrayList}.
*/
@Override public int size() {
return size;
}因此取size方法时,会增加
/**
* Constructs a new {@code ArrayList} instance with zero initial capacity.
*/
public ArrayList() {
array = EmptyArray.OBJECT;
}而第二个对list进行操作,只是初始化了list对象,只是改变它的值,没改变全局变量的size,而调用clear方法有效
/**
* Removes all elements from this {@code ArrayList}, leaving it empty.
*
* @see #isEmpty
* @see #size
*/
@Override public void clear() {
if (size != 0) {
Arrays.fill(array, 0, size, null);
size = 0;
modCount++;
}
}再看StringBuffer
/**
* Adds the character array to the end of this buffer.
*
* @param chars
* the character array to append.
* @return this StringBuffer.
* @throws NullPointerException
* if {@code chars} is {@code null}.
*/
public synchronized StringBuffer append(char[] chars) {
append0(chars);
return this;
}final void append0(String string) {
if (string == null) {
appendNull();
return;
}
int length = string.length();
int newCount = count + length;
if (newCount > value.length) {
enlargeBuffer(newCount);
}
string._getChars(0, length, value, count);
count = newCount;
}变量变化时会调用底层,相当于把栈的值改变,而引用仍指向这个对象,当然会它的值会变了
Java数据传递实验的更多相关文章
- java数据传递例子+内存分析
一.引用传递 1.例子1 package com.jikexueyuan.ref; class Ref1{ int temp = 10; } public class RefDemo01 { publ ...
- Java 前台后台数据传递、中文乱码解决方法
1.向前台传递数据;2.向后台传递数据;3.ajax post 提交数据到服务端时中文乱码解决方法;4.数组类型参数传递; 1.向前台传递数据:1.1 字符串数据传递: 这种方式只是单一的向前台传递 ...
- Java WEB中的HttpServletResponse数据传递
1.什么是HttpServletResponse 2.使用HttpServletResponse向浏览器发送数据及相关实例. 实例1:实现文件下载功能 实例2:实现验证码注册 实例3:实现页面3秒后跳 ...
- Java多线程学习---------超详细总结(java 多线程 同步 数据传递 )
目录(?)[-] 一扩展javalangThread类 二实现javalangRunnable接口 三Thread和Runnable的区别 四线程状态转换 五线程调度 六常用函数说明 使用方式 为什么 ...
- Android(java)学习笔记220:开发一个多界面的应用程序之界面间数据传递
1.界面跳转的数据传递 (1)intent.setData() --> intent.getData(): 传递的数据比较简单,一般是文本类型的数据String:倘若我们传递的数据比较复 ...
- 《Java从入门到放弃》入门篇:springMVC数据传递
springMVC中的数据传递方式与JSP和Struts2相比,更加的简单.具体有什么样的区别呢?我们通过下面这张图来对比就知道了. 随手画的,有些错别字,不用太在意..... 接下来,进入正题,sp ...
- Java并发:线程间数据传递和交换
转自:https://www.cnblogs.com/java-zzl/p/9741288.html 一.通过SynchronousQueue方式实现线程间数据传递: 线程A与线程B共同持有一个Syn ...
- 【Java框架型项目从入门到装逼】第十一节 用户新增之把数据传递到后台
让我们继续来做"主线任务",这一节,我们来做具体的用户新增功能.首先,为了简单起见,我把主页面改了一些,改的是列表那一块.删去了一些字段,和数据库表对应一致: 现在,我们要实现一个 ...
- Android(java)学习笔记163:开发一个多界面的应用程序之界面间数据传递
1.界面跳转的数据传递 (1)intent.setData() --> intent.getData(): 传递的数据比较简单,一般是文本类型的数据String:倘若我们传递的数据比较复 ...
随机推荐
- ES6学习笔记(十六)async函数
1.含义 ES2017 标准引入了 async 函数,使得异步操作变得更加方便. async 函数是什么?一句话,它就是 Generator 函数的语法糖,号称异步的终极解决方案. 前文有一个 Gen ...
- TP5 上传文件
直接贴上一个完整的代码 /** * 图片上传方法 * @return [type] [description] */ /** * 1 获取到文件 * 2 验证文件的形状是不是符合上传的规则 * 3 i ...
- php后端控制可跨域的域名,允许图片跨域上传
跨域问题经常需要面对,前端需要做的比较直接要么选择ajax异步提交,XML或者jsonp,要么表单提交前端常见跨域解决方案 jsonp基本可以搞定大部分跨域问题,但问题也比较明显,只能通过get方式提 ...
- Python组织文件 实践:拷贝某种类型的所有文件
#! python3 #chapter09-test01- 遍历目录树,查找特定扩展名的文件不论这些文件的位置在哪里,都将他们 #拷贝到一个新的文件夹中 import os,shutil,pprint ...
- 紫书 例题 10-17 UVa 1639(数学期望+对数保存精度)
设置最后打开的是盒子1, 另外一个盒子剩下i个 那么在这之前打开了n + n - i次盒子 那么这个时候的概率是C(2 * n - i, n) p ^ (n+1) (1-p)^ (n - i) 那么反 ...
- Android通过XML来定义Menu
直接在代码中添加菜单项,给菜单项分组等,这是比较传统的做法,它存在着一些不足.比如说,为了响应每个菜单项,我们需要用常量来保存每个菜单项的ID等.为此,Android提供了一种更好的方式,就是把men ...
- Unity Shader (二)Cg语言
一.Cg基本数据类型 float 32位浮点数 half 16位浮点数 int 32位整型 fixed 12位定点数 bool 布尔数据 simpler* 纹理对象的句柄( the handle to ...
- 【转】 Java 进行 RSA 加解密时不得不考虑到的那些事儿
[转] Java 进行 RSA 加解密时不得不考虑到的那些事儿 1. 加密的系统不要具备解密的功能,否则 RSA 可能不太合适 公钥加密,私钥解密.加密的系统和解密的系统分开部署,加密的系统不应该同时 ...
- 【转】30分钟掌握 C#6
[转]30分钟掌握 C#6 1. 只读自动属性(Read-only auto-properties) C# 6之前我们构建只读自动属性: public string FirstName { get; ...
- LiquiBase预判断
预判断解决的问题:运行liquibase之前,DB中已经存在一个table,所以需要加上预判断: 完整的一个例子: <?xml version="1.0" encoding= ...