| |

VerySource

 Forgot password?
 Register
Search
Author: 阿迪雅

I still don't understand the object reference

[Copy link]

0

Threads

14

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 Invalid IP Address

Post time: 2020-7-18 12:45:01
| Show all posts
Hey, comrades study hard!
Reply

Use magic Report

0

Threads

14

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 Invalid IP Address

Post time: 2020-7-18 16:00:01
| Show all posts
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").
Reply

Use magic Report

0

Threads

3

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-7-18 16:30:02
| Show all posts
I don’t know if I’m right, everyone
Because what is actually passed is a copy of the reference, so,
    Base base = new Base(3);
    changeBase(base);
The base reference only changes one attribute of the object, but does not change the object.
    String str="1234";
    changeStr(str);
The copy of the str object has been rewritten to point to the string object "welcome", but in fact str has not been modified, so the printout is still 1234. If you add a print statement to the changeStr(str) function, it is estimated that welcome will be printed
Reply

Use magic Report

0

Threads

14

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 Invalid IP Address

Post time: 2020-7-18 17:00:01
| Show all posts
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).
Reply

Use magic Report

0

Threads

3

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-7-18 17:15:01
| Show all posts
my suggestion:

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

Use magic Report

0

Threads

14

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 Invalid IP Address

Post time: 2020-7-18 18:15:01
| Show all posts
Good suggestion.

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

Use magic Report

1

Threads

7

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

Post time: 2020-7-19 12:00:01
| Show all posts
String can be regarded as the same as the general basic data type, which is passed by value
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list