Java Basics - value transfer

Value is passed?

Copies the value passed as a parameter, the parameter changes without affecting the original copy.

Pass by reference?

Argument passed is the parameter itself, parameter change, it is also changed argument.

Java What is pass?

Java only passed by value

Why not do the actual situation?

  1. The basic data types

  

 1 public class Main {
 2 
 3 
 4     void method01(int a ){
 5         a++;
 6         System.out.println("a in method01 is "+a);
 7     }
 8 
 9     public static void main(String[] args) {
10        Main main = new Main();
11        int a = 12;
12        
13        main.method01(a);
14        System.out.println(a);
15 
16 
17     }
18 }

Output:

a in method01 is 13
12

No problem, the original value remains unchanged.

  2. String type

 1 public class Main {
 2 
 3     void method02(String s){
 4         s = "world";
 5         System.out.println("string in method is "+s);
 6    
 7     }
 8 
 9 
10     public static void main(String[] args) {
11        Main main = new Main();
12    
13        String s = "hello";
14 
15        main.method02(s);
16        System.out.println(s);
17 
18 
19     }
20 }

Output:

string in method is world
hello

No problem, is passed by value. Original value has not changed

  3. Custom Class

public class Main {
    void method03(Cat cat){
        cat.setName("haha");
        System.out.println("cats name is "+cat.getName());
    }

    public static void main(String[] args) {
       Main main = new Main();

        Cat cat = new Cat();
        cat.setName("mimi");
        main.method03(cat);
        System.out.println("cats name in main is "+cat.getName());

    }
}
class Cat{
    String name;

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }
}

Output:

cats name is haha
cats name in main is haha

wrong! Why change the original value of the value is passed? ? ? ? ? ?

Note that, here, although the original value has changed, but still passed by value. why?

Because the transmission is a copy of a reference parameter value is also transmitted, the value herein refers to a copy of the address.

To give a simple example (the reference address: https://www.cnblogs.com/wchxj/p/8729503.html )

Value is passed: Do you have a key, when your friends want to go to your home, you engraved a new key to him, still in his own hands, which is passed by value. In this case, he put what key do this will not affect this key in your hand.

Passed by reference: Do you have a key, when your friends want to go to your home, if you put your keys directly to him, which is passed by reference. In this case, the key to this if he did anything like he carved his name on a key, then this key back to you, on your own keys will be more out of his engraved name.

When you use the value passed, a copy of the key to your friend, your friend smashed TV in your home, then you go home when the TV is definitely smashed.

This example is much easier to understand. According to this logic we have to test it.

Copy if Java passed just cited, we point to the parameter null, then the argument will not be affected. By the above example to explain it is that if you give your friend the keys just a duplicate key, we will destroy the key hands of a friend, we pass the original key to open the door.

 

public  class the Main { 

    void method04 (Cat CAT) { 
    // destroy key friends hands CAT
= null ; the try { System.out.println (cat.getName ()); } the catch (Exception E) { e.printStackTrace (); System.out.println ( "CAT has been recovered!" ); } } public static void main (String [] args) { the main main = new new the main (); Cat CAT = new new Cat (); cat.setName ( "Mimi "); main.method04(cat); System.out.println(cat.getName()); } } class Cat{ String name; public void setName(String name) { this.name = name; } public String getName() { return name; }

Output:

java.lang.NullPointerException
at Main.method04(Main.java:21)
at Main.main(Main.java:44)
cat 已经被回收!
new name

Yes! That's it.

So Java is only passed by value.

Guess you like

Origin www.cnblogs.com/liziweiblog/p/11079164.html