VerySource

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
12
返回列表 发新帖
楼主: 阿迪雅

关于对象引用,还是没搞明白

[复制链接]

0

主题

14

帖子

9.00

积分

新手上路

Rank: 1

积分
9.00
发表于 2020-7-18 12:45:01 | 显示全部楼层
哎,同志们好好读书!
回复

使用道具 举报

0

主题

14

帖子

9.00

积分

新手上路

Rank: 1

积分
9.00
发表于 2020-7-18 16:00:01 | 显示全部楼层
1, Java uses "Pass by Value" in method argument passing.

For primitive variables, the actual primitive value is stored.

For reference variables, which refer to instances of classes, the object reference is stored. This object reference is also called "pointer to the actual Object instance" or "the way (or the path) to reach the actual Object instance".

When a primitive variable is passed as an argument to a method, a separated copy of the variable value is passed to the method. This copy of the value can be changed within the called method, but the orginal primitive variable is not affected.

When a reference variable is passed as an argument to a method, a separated copy of the varialbe value is passed to the method as well. Over here, this variable value is the object reference (or the way to reach the actual Object instance). It's NOT the actual Object instance. The called method knows this object reference (the way to reach the original Object instance), so it can reach the original Object instance. This is why the called method can change the original Object instance (only if the Object instance is mutable).

Let's see the example given. Reference variable "base" is pointing a mutable "Base" instance. That's why the "changeBase" method changed the original "Base" instance.

2, String class is final, which means String instances are immutable. If a String reference variable is passed as an argument to a method, the method can reach the original String instance, but it can NEVER change it.

In the given example, String variable "str" is assigned with value "1234". When "str" is passed in to method "changeStr", a copy of this String reference varialbe is passed to method "changeStr". Let's call this copy with the name "strCopy". Now, both "str" and "strCopy" point to the same String instance (value "1234"). Inside method "changeStr", String reference variable "str" (which is actually "strCopy") is assigned with new value, which points to another String instance (value "welcome"). The original String reference variable is not affected. It still points to the String instance (value "1234").
回复

使用道具 举报

0

主题

3

帖子

2.00

积分

新手上路

Rank: 1

积分
2.00
发表于 2020-7-18 16:30:02 | 显示全部楼层
不知道我说的对不对,大家来说下
因为实际上传递的是引用的副本,所以的话,
    Base base= new Base(3);
    changeBase(base);
base引用只改变了对象的一个属性,并没有改变对象,
    String str="1234";
    changeStr(str);
str对象的副本已经被重写指向了“welcome”这个string对象,但是实际上str还是没有被修改,所以打印出来的还是1234,如果在changeStr(str)函数里增加一条打印语句估计会打印出welcome
回复

使用道具 举报

0

主题

14

帖子

9.00

积分

新手上路

Rank: 1

积分
9.00
发表于 2020-7-18 17:00:01 | 显示全部楼层
3, Let's see another example here. Let's reuse the code above.
-----------------------------------
class Base
{
public int i;
public Base(int a)
{
i=a;
}
}

public class StringTest {
  public static void changeBase(Base b)
  {
      b = new Base(7);
      System.out.println("Inside: " + b.i);
  }
  
  public static void main(String[] args) {
    Base base= new Base(3);
    System.out.println("Before: " + base.i);
    changeBase(base);
    System.out.println("After: " + base.i);
  }
}
-------------------------------------

What's the output?
A:
Before: 3
Inside: 7
After: 7

B:
Before: 3
Inside: 7
After: 3

The actual answer here is B.

Inside method "changeBase", Base reference variable b is initially points to the same Base instance (whose i is 3) as "base". Then it is reassigned with a new value, which points to a new Base instance (whose i is 7). The original Base reference variable "base' is not touched, nor does the original Base instance (whose i is 3).
回复

使用道具 举报

0

主题

3

帖子

2.00

积分

新手上路

Rank: 1

积分
2.00
发表于 2020-7-18 17:15:01 | 显示全部楼层
my suggestion:

System.out.println("Inside: " + b);
System.out.println("After: " + base);
回复

使用道具 举报

0

主题

14

帖子

9.00

积分

新手上路

Rank: 1

积分
9.00
发表于 2020-7-18 18:15:01 | 显示全部楼层
Good suggestion.

It will be much better if you take the initiative to explain what the output is and why it is so. :-D
回复

使用道具 举报

1

主题

7

帖子

6.00

积分

新手上路

Rank: 1

积分
6.00
发表于 2020-7-19 12:00:01 | 显示全部楼层
String 就可以看成和一般的基础数据类型一样 ,是值传递的
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

快速回复 返回顶部 返回列表