骚年,Java值传递你会吗

骚年,Java值传递你会吗

值传递和引用传递

1、值传递是通过方法对传入值操作后,并不会改变传入的值

2、引用传递是通过方法对传入的值操作后,会改变传入的引用指向数据

Java中的值传递

Talk is cheap, show your code。

/**
 * @author : cuantianhou 2020/4/30
 */

public class Student {

    public String getName() {
        return name;
    }

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

    @Override
    public String toString() {
        return "Student{" +
        "name='" + name + '\'' +
        '}';
    }

    private String name;
}
/**
 * @author : cuantianhou 2020/4/30
 */
public class ValueTransfer {


    public static void swap(int a, int b){
        a = a ^ b;
        b = a ^ b;
        a = a ^b;
        System.out.println("swap after"+a + ","+b);
    }

    public static void swapObject(Student a ,Student b){
        Student student = a;
        a = b;
        b = student;
        System.out.println("swap after"+a + ","+b);
    }
    public static void main(String[] args) {
        int a =  1;
        int b =  2;
        System.out.println("swap before"+a + ","+b);
        swap(a,b);
        Student sa = new Student();
        sa.setName("sa");
        Student sb = new Student();
        sb.setName("sb");
        System.out.println("swap before"+sa + ","+sb);
        swapObject(sa,sb);

    }
}

然后我们来看下测试结果
在这里插入图片描述
优秀的你,肯定会觉得太简单了,傻子都会,你不会是标题党吧。下面我们来看下一个场景:比如我们需要去调用一个接口,该接口会返回一个对象,然后你使用方法参数接收返回的对象,自以为已经改变方法中传入对象属性的值,那我们来模拟验证下。

/**
 * @author : cuantianhou 2020/4/30
 */
public class RemoteCallClass {


    public Student remoteCall(){
        Student student = new Student();
        student.setName("remote");
        return student;
    }
}
/**
 * @author : cuantianhou 2020/4/30
 */
public class RemoteMain {


    public static void getStudent(Student studnet){
        RemoteCallClass remoteCallClass = new RemoteCallClass();
        studnet = remoteCallClass.remoteCall();
    }

    public static void main(String[] args) {
        Student  student = new Student();
        getStudent(student);
        System.out.println(student);
    }
}

我们来看下模拟结果:
在这里插入图片描述
此时只想说:小朋友你是否有很多问号,那为什么会出现这种现象呢,来上图。

在这里插入图片描述
这个是在不同时机,student指向的内存图,getStudent中的student通过返回对象,指向了RemoteCallClass中指向的这个对象,getStudnet的student没有返回值,所以Main方法中的student这是值传递的一个变种,平时需要注意,主要是针对小白。那如何来解决这种问题呢?主要有两种方法:第一我们在传递的时候改变对象的属性;
修改后代码如下:

public static void getStudent(Student studnet){
    RemoteCallClass remoteCallClass = new RemoteCallClass();
    studnet.setName(remoteCallClass.remoteCall().getName());
    System.out.println("remote"+studnet);
}

测试结果:
在这里插入图片描述

第二,将赋值的对象返回到调用方法中。
修改后的代码如下:

public static Student getStudent(Student studnet){
    RemoteCallClass remoteCallClass = new RemoteCallClass();
    studnet = remoteCallClass.remoteCall();
    System.out.println("remote"+studnet);
    return studnet;
}

public static void main(String[] args) {
    Student  student = new Student();
    student=getStudent(student);
    System.out.println(student);
}

测试结果如下:

在这里插入图片描述

总结

每天进步一点点,祝各位前程似锦,觉得不错,点个赞再走呗。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值