---------siwuxie095

引用传递:

参考链接1参考链接2参考链接3参考链接4

代码1

package com.siwuxie095.ref;

class Ref1{

int temp=10;

}

public class RefDemo01 {

public static void main(String[] args) {

Ref1 r1= new Ref1();

r1.temp=20;

System.out.println(r1.temp);

tell(r1);

System.out.println(r1.temp);

}

//引用传递

public static void tell(Ref1 r2) {

//r2在改变temp的值为30之前,和r1都指向了 20

r2.temp=30;

}

}

运行一览:

内存分析:

代码2

package com.siwuxie095.ref;

public class RefDemo02 {

public static void main(String[] args) {

String str1="hello";

System.out.println(str1);

tell(str1);

System.out.println(str1);

}

public static void tell(String str2) {

str2="jike";

}

}

运行一览:

因为String类型的数据是不可更改的

内存分析:

代码3:

package com.siwuxie095.ref;

class Ref3{

String temp="hello";

}

public class RefDemo03 {

public static void main(String[] args) {

Ref3 r1=new Ref3();

r1.temp="jike";

System.out.println(r1.temp);

tell(r1);

System.out.println(r1.temp);

}

public static void tell(Ref3 r2) {

r2.temp="xueyuan";

}

}

运行一览:

实际上是开辟了新的内存空间(与代码2对比)

内存分析:

【made by siwuxie095】

优质内容筛选与推荐>>
1、jmeter-显示log的方法,和脚本通用的语法
2、用浏览器返回到上一页时,上一页刷新
3、C/C++字符串使用整理
4、C语言博客作业02--循环结构
5、swift的类型系统


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号