| |

VerySource

 Forgot password?
 Register
Search
View: 2251|Reply: 16

I still don't understand the object reference

[Copy link]

2

Threads

16

Posts

11.00

Credits

Newbie

Rank: 1

Credits
11.00

 China

Post time: 2020-3-24 10:00:01
| Show all posts |Read mode
class Base
{
public int i;
public Base (int a)
{
i = a;
}
}

public class StringTest {
  public static void changeStr (String str) {
    str = "welcome";
  }
  public static void changeBase (Base b)
  {
  b.i = 6;
  }
  
  public static void main (String [] args) {
    String str = "1234";
    Base base = new Base (3);
    changeBase (base);
    System.out.println (base.i);
    changeStr (str);
    System.out.println (str);
  }
}

I thought I understood, but I still did n’t understand
changeBase modified the value of i according to the "object reference" rule,
But changeStr did not follow the "object reference" rule, still "1234"
why?
Reply

Use magic Report

0

Threads

1

Posts

0.00

Credits

Newbie

Rank: 1

Credits
0.00

 China

Post time: 2020-3-24 13:58:36
| Show all posts
class Base
{
        public int i;
        public Base (int a)
        {
                i = a;
        }
}

public class StringTest {
  public static void changeStr (String str) {
    str = "welcome";
  }
  public static void changeBase (Base b)
  {
          b.i = 6;
  }
  
  public static void main (String [] args) {
    String str = "1234";
    Base base = new Base (3);
    changeBase (base);
    System.out.println (base.i);
    changeStr (str);
    System.out.println (str);
  }
}
Reply

Use magic Report

0

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-7-3 21:45:02
| Show all posts
String objects are a bit special
Once a String object is generated
You can never change it
Reply

Use magic Report

1

Threads

51

Posts

32.00

Credits

Newbie

Rank: 1

Credits
32.00

 China

Post time: 2020-7-4 01:00:01
| Show all posts
The first change is to pass the object in, the "object reference" rule.
There is no transfer address in JAVA, so it is still "1234"
Reply

Use magic Report

1

Threads

20

Posts

15.00

Credits

Newbie

Rank: 1

Credits
15.00

 China

Post time: 2020-7-5 23:15:02
| Show all posts
The first one is that object b object b cannot be changed
The second one is that string string cannot be changed directly
Reply

Use magic Report

0

Threads

5

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-7-6 11:00:02
| Show all posts
You can change it like this
  public static String changeStr(String str){
    return "welcome";
  }
  public static void changeBase(Base b)
  {
  b.i=6;
  }
  
  public static void main(String[] args) {
    String str="1234";
    Base base = new Base(3);
    changeBase(base);
    System.out.println(base.i);
    str = changeStr(str);
    System.out.println(str);
  }

String is an immutable class and must display the result of the change
Reply

Use magic Report

2

Threads

16

Posts

11.00

Credits

Newbie

Rank: 1

Credits
11.00

 China

 Author| Post time: 2020-7-7 11:15:01
| Show all posts
Upstairs, I will try to quote what is the use of your return value

Upstairs upstairs:
The first one is that object b object b cannot be changed // but b has changed
The second one is that string string can't be directly changed naturally // this sentence is derived from the previous sentence, so this sentence is also not true
Reply

Use magic Report

0

Threads

4

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-7-7 22:45:01
| Show all posts
String and basic types are all fianl type! You can look at the book of reference passing and value passing
Reply

Use magic Report

1

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-7-17 21:45:01
| Show all posts
str = changeStr(str); The effect of the statement is actually equivalent to

str = "welcome"; The return value is directly assigned to str, so the string displayed changes
Reply

Use magic Report

3

Threads

17

Posts

14.00

Credits

Newbie

Rank: 1

Credits
14.00

 China

Post time: 2020-7-17 23:45:01
| Show all posts
I understand it this way: what changed is a copy of str
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