本文来自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数据传递实验的更多相关文章

  1. java数据传递例子+内存分析

    一.引用传递 1.例子1 package com.jikexueyuan.ref; class Ref1{ int temp = 10; } public class RefDemo01 { publ ...

  2. Java 前台后台数据传递、中文乱码解决方法

    1.向前台传递数据;2.向后台传递数据;3.ajax post 提交数据到服务端时中文乱码解决方法;4.数组类型参数传递; 1.向前台传递数据:1.1 字符串数据传递:  这种方式只是单一的向前台传递 ...

  3. Java WEB中的HttpServletResponse数据传递

    1.什么是HttpServletResponse 2.使用HttpServletResponse向浏览器发送数据及相关实例. 实例1:实现文件下载功能 实例2:实现验证码注册 实例3:实现页面3秒后跳 ...

  4. Java多线程学习---------超详细总结(java 多线程 同步 数据传递 )

    目录(?)[-] 一扩展javalangThread类 二实现javalangRunnable接口 三Thread和Runnable的区别 四线程状态转换 五线程调度 六常用函数说明 使用方式 为什么 ...

  5. Android(java)学习笔记220:开发一个多界面的应用程序之界面间数据传递

    1.界面跳转的数据传递 (1)intent.setData() --> intent.getData():     传递的数据比较简单,一般是文本类型的数据String:倘若我们传递的数据比较复 ...

  6. 《Java从入门到放弃》入门篇:springMVC数据传递

    springMVC中的数据传递方式与JSP和Struts2相比,更加的简单.具体有什么样的区别呢?我们通过下面这张图来对比就知道了. 随手画的,有些错别字,不用太在意..... 接下来,进入正题,sp ...

  7. Java并发:线程间数据传递和交换

    转自:https://www.cnblogs.com/java-zzl/p/9741288.html 一.通过SynchronousQueue方式实现线程间数据传递: 线程A与线程B共同持有一个Syn ...

  8. 【Java框架型项目从入门到装逼】第十一节 用户新增之把数据传递到后台

    让我们继续来做"主线任务",这一节,我们来做具体的用户新增功能.首先,为了简单起见,我把主页面改了一些,改的是列表那一块.删去了一些字段,和数据库表对应一致: 现在,我们要实现一个 ...

  9. Android(java)学习笔记163:开发一个多界面的应用程序之界面间数据传递

    1.界面跳转的数据传递 (1)intent.setData() --> intent.getData():     传递的数据比较简单,一般是文本类型的数据String:倘若我们传递的数据比较复 ...

随机推荐

  1. 消除textarea的空格de长度值

    在项目中因为用到文本域textarea输入textarea的长度总是显示 25 那是还怀疑textarea自带有value长度? placeholder属性的长度? 那时候想到类似:ul无序列表li元 ...

  2. whoami---打印当前有效的用户名称

    whoami命令用于打印当前有效的用户名称,相当于执行id -un命令. 语法 whoami(选项) 选项 --help:在线帮助: --version:显示版本信息. 实例 [root@localh ...

  3. java开发微信公众号支付(JSAPI)

    https://www.cnblogs.com/gopark/p/9394951.html,这篇文章写的已经很详细了. 下面写一下自己的思路: 1.首先下载demo,地址:https://pay.we ...

  4. WHU 1537 Stones I

    题目见: http://acm.whu.edu.cn/land/problem/detail?problem_id=1537 这个题相当无语,学长给的解法是:枚举取的个数k,然后对每个k贪心,取其中的 ...

  5. qt 闰年

    bool QDate::isLeapYear ( int year ) [static]

  6. Java.Lang.NoSuchMethod 错误

    项目开发.调用webservice,方法调用报了 Java.Lang.NoSucheMethod..........,印象中记得是jar包冲突,maven项目,一看,这一堆jar包...用eclips ...

  7. 分享:FIFO 同步、异步以及Verilog代码实现

    FIFO 很重要,之前参加的各类电子公司的逻辑设计的笔试几乎都会考到. FIFO是英文First In First Out 的缩写,是一种先进先出的数据缓存器,他与普通存储器的区别是没有外部读写地址线 ...

  8. Anatomy of an IIS7 configuration path

    The concept of configuration paths is fundamental to managing and operating an IIS server, so I want ...

  9. Linux下清除系统日志方法

    摘要:相信大家都是用过Windows的人.对于Windows下饱受诟病的各种垃圾文件都需要自己想办法删除,不然你的系统将会变得越来越大,越来越迟钝!windows怎么清理垃圾相信大家都知道的,那么li ...

  10. iOS基本UI控件总结

    包括以下几类: //继承自NSObject:(暂列为控件) UIColor *_color;    //颜色 UIImage *_image;    //图像 //继承自UIView:只能相应手势UI ...