前言
最近公司发生了一个死循环,究其原因是由于 引用传递 和 值传递 没有弄清楚,导致一直没有跳出循环。所以就打算分享一下这个知识点,希望对大家有所帮助!
视频链接
代码示例
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++);
}
}