切记,JAVA中只有值传递,没有引用传递

文章通过示例代码详细解释了Java中只有值传递这一特点,分别演示了基本数据类型、对象引用以及List类型的参数传递行为。在基本数据类型中,传递的是副本;对于对象,传递的是引用副本,导致对对象的修改会影响到原对象;而对于List,即使在方法内创建新的List,原引用也不会改变。
摘要由CSDN通过智能技术生成

前言

最近公司发生了一个死循环,究其原因是由于 引用传递 和 值传递 没有弄清楚,导致一直没有跳出循环。所以就打算分享一下这个知识点,希望对大家有所帮助!

视频链接

B站视频详解

代码示例

package org.example;

import com.alibaba.fastjson.JSON;
import org.openjdk.jol.vm.VM;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 引用传递和值传递
 * JAVA中只有【值传递】
 */
public class App {

    public static void main(String[] args) {
        //测试基本数据类型-int
        //testInt();
        //测试引用数据类型-Obj
        //testObj();
        //测试引用数据类型-List
        testList();
    }

    private static void testInt() {
        int a = 0;
        int b = 0;

        System.out.println("传入handleInt前的地址:" + VM.current().addressOf(a));

        handleInt(a,b);

        System.out.println("testInt-a:" + a);
        System.out.println("testInt-b:" + b);
    }

    private static void handleInt(int a, int b) {

        System.out.println("传入handleInt后的地址:" + VM.current().addressOf(a));

        a = a + 1;
        b = a;

        System.out.println("在handleInt里面进行赋值后的地址:" + VM.current().addressOf(a));

        System.out.println("handleInt-a 第一次:" + a);
        System.out.println("handleInt-b 第一次:" + b);

        b = 10;
        a = b;

        System.out.println("handleInt-b 第二次:" + b);
        System.out.println("handleInt-a 第二次:" + a);
    }

    private static void testObj() {
        Bill billOne = new Bill(BigInteger.valueOf(1L), "张三", new Date());
        Bill billTwo = new Bill(BigInteger.valueOf(2L), "李四", new Date());

        handleObj(billOne,billTwo);

        System.out.println("testObj-billOne:" + JSON.toJSONString(billOne));
        System.out.println("testObj-billTwo:" + JSON.toJSONString(billTwo));
    }

    private static void handleObj(Bill billOne, Bill billTwo) {
        //billOne = new Bill(BigInteger.valueOf(3L), "王五", new Date());
        //
        //System.out.println("handleObj-billOne:第一次" + JSON.toJSONString(billOne));
        //System.out.println("handleObj-billTwo:第一次" + JSON.toJSONString(billTwo));

        billOne.setBuyer("小刘");
        billTwo = billOne;

        System.out.println("handleObj-billOne:第二次" + JSON.toJSONString(billOne));
        System.out.println("handleObj-billTwo:第二次" + JSON.toJSONString(billTwo));
    }

    private static void testList() {
        List<Bill> bills = queryBillAll();
        handleList(bills);
        System.out.println("testList-bills handleList:" + JSON.toJSONString(bills));

        useList(bills);
        System.out.println("testList-bills useList:" + JSON.toJSONString(bills));
    }

    private static void handleList(List<Bill> bills) {
        bills = new ArrayList<>();
        System.out.println("handleList-bills:" + JSON.toJSONString(bills));
    }

    private static void useList(List<Bill> bills) {
        bills.add(new Bill(BigInteger.valueOf(99L),"9999",new Date()));
    }

    /*----- 模拟数据查询 ------*/

    private static List<Bill> queryBillAll(){
        List<Bill> billList = queryBillList();
        List<Detail> detailList = queryDetailList();

        Map<BigInteger, List<Detail>> detailListMap = detailList.stream().collect(Collectors.groupingBy(Detail::getBillId));
        for (Bill bill : billList) {
            List<Detail> details = detailListMap.get(bill.getId());
            if (null!=details && details.size()>0) {
                bill.setDetailList(details);
            }
        }

        return billList;
    }

    private static List<Bill> queryBillList() {
        //模拟SQL查询主表数据
        List<Bill> billList = new ArrayList<>();
        billList.add(new Bill(BigInteger.valueOf(1L),"张三",new Date()));
        billList.add(new Bill(BigInteger.valueOf(2L),"李四",new Date()));
        billList.add(new Bill(BigInteger.valueOf(3L),"王五",new Date()));
        billList.add(new Bill(BigInteger.valueOf(4L),"小张",new Date()));
        billList.add(new Bill(BigInteger.valueOf(5L),"小李",new Date()));
        return billList;
    }

    private static List<Detail> queryDetailList() {
        //模拟SQL查询明细数据
        List<Detail> detailList = new ArrayList<>();
        detailList.add(new Detail(BigInteger.valueOf(1L),BigInteger.valueOf(11L),"衣服",1));
        detailList.add(new Detail(BigInteger.valueOf(1L),BigInteger.valueOf(12L),"裤子",1));
        detailList.add(new Detail(BigInteger.valueOf(2L),BigInteger.valueOf(21L),"鞋子",2));
        detailList.add(new Detail(BigInteger.valueOf(2L),BigInteger.valueOf(22L),"衣服",1));
        detailList.add(new Detail(BigInteger.valueOf(3L),BigInteger.valueOf(31L),"手表",2));
        detailList.add(new Detail(BigInteger.valueOf(4L),BigInteger.valueOf(41L),"手机",1));
        detailList.add(new Detail(BigInteger.valueOf(5L),BigInteger.valueOf(51L),"耳机",2));
        detailList.add(new Detail(BigInteger.valueOf(5L),BigInteger.valueOf(51L),"电脑",1));
        return detailList;
    }

}

/**
 * 订单主表
 */
class Bill {
    /**
     * 订单ID
     */
    private BigInteger id;
    /**
     * 买家姓名
     */
    private String buyer;
    /**
     * 发货时间
     */
    private Date sendDate;
    /**
     * 订单明细
     */
    private List<Detail> detailList;

    public Bill(BigInteger id, String buyer, Date sendDate) {
        this.id = id;
        this.buyer = buyer;
        this.sendDate = sendDate;
    }

    public BigInteger getId() {
        return id;
    }

    public void setId(BigInteger id) {
        this.id = id;
    }

    public String getBuyer() {
        return buyer;
    }

    public void setBuyer(String buyer) {
        this.buyer = buyer;
    }

    public Date getSendDate() {
        return sendDate;
    }

    public void setSendDate(Date sendDate) {
        this.sendDate = sendDate;
    }

    public List<Detail> getDetailList() {
        return detailList;
    }

    public void setDetailList(List<Detail> detailList) {
        this.detailList = detailList;
    }
}

/**
 * 订单明细
 */
class Detail {
    /**
     * 订单主表ID
     */
    private BigInteger billId;
    /**
     * 订单明细ID
     */
    private BigInteger detailId;
    /**
     * 商品名称
     */
    private String name;
    /**
     * 商品数量
     */
    private Integer qty;

    public Detail(BigInteger billId, BigInteger detailId, String name, Integer qty) {
        this.billId = billId;
        this.detailId = detailId;
        this.name = name;
        this.qty = qty;
    }

    public BigInteger getBillId() {
        return billId;
    }

    public void setBillId(BigInteger billId) {
        this.billId = billId;
    }

    public BigInteger getDetailId() {
        return detailId;
    }

    public void setDetailId(BigInteger detailId) {
        this.detailId = detailId;
    }

    public String getName() {
        return name;
    }

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

    public Integer getQty() {
        return qty;
    }

    public void setQty(Integer qty) {
        this.qty = qty;
    }
}

package org.example;

/**
 * 死循环 i++
 */
public class TestOther {

    public static void main(String[] args) {
        int index = 0;
        handle(index);
    }

    public static void handle(int index){
        // 0
        System.out.println("传入的index:" + index);
        System.out.println("处理业务。。。");
        if(index > 1){
            return;
        }
        handle(index++);
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员四海

道阻且长,行则将至

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值