关于java中“=”的思考(java引用)

本文探讨了Java中对象引用的工作原理,并通过链表反转算法深入解析了引用的行为。通过两个示例说明了基础类型与非基础类型的赋值区别。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

关于java中“=”的思考(java引用)


事由

package com.mryao.node;

public class Node<E> {
    E item;
    Node<E> next;

    Node(E element){
        this.item = element;
        this.next = null;
    }

    public static void printListRev(Node<String> head){
        if(head != null){
            printListRev(head.next);
            System.out.println(head.item);
        }
    }

    public static Node<String> revList(Node<String> head){
        if(head == null){
            return null;
        }
        Node<String> nodeResult = null;
        Node<String> nodePre = null;
        Node<String> current = head;
        while (current != null){
            Node<String> nodeNext = current.next;
            if(nodeNext == null){
                nodeResult = current;
//                nodeResult.next = nodePre;为什么这一句可以注释掉 不影响反转链表  按照我的理解 这里需要这句话
            }
            current.next = nodePre; // debug跟 发现在这里current.next 变化 会引发nodeResult.next变化
            nodePre = current;
            current = nodeNext;//那为什么这里 current 变了  上面的nodeResult没有变化?
        }
        return nodeResult;
    }

    public static void main(String[] args){
        Node<String> head = null;
        Node<String> tail = null;
        head = new Node("1");
        tail = head;

        tail.next = new Node("2");
        tail = tail.next;
        tail.next = new Node("3");
        tail = tail.next;
        tail.next = new Node("4");
        tail = tail.next;

        Node<String> current = head;
        System.out.println("print node :");
        while (current != null){
            System.out.println(current.item);
            current = current.next;
        }
        System.out.println("print node rev :");
        Node.printListRev(head);
        Node.revList(head);
    }
}

按照博主的思路,在最后一步的判断当中,应该是要有nodeResult.next = nodePre;这一句的,后来网上参考了一些其他的写法 ,发现好像不需要,结果注释掉尝试了一下,居然可以正常运行


这怎么行呢 ?赶紧debug跟着走了一遍,发现了原因所在。


在这个地方current 和 resultNode是指向同一个对象
所以在后面的current.next改变时,两者都会变化
而在最后的current=nodeNext,只是改变了current的指向对象(null)
所以不会影响 resultNode


这让我意识到java的对象的引用的问题于是尝试用一些简单的例子来加深对这方面的理解


例子一

    public static void main(String [] args){
        String a = "1";
        String b = a;
        String c = b;
        String d = b;
        a = "a";
        b = "b";
        System.out.println("a " + a);
        System.out.println("b " + b);
        System.out.println("c " + c);
        System.out.println("d " + d);
    }

输出

a a
b b
c 1
d 1

例子二

package com.mryao;

public class A {
    private String a;

    public String getA() {
        return a;
    }

    public void setA(String a) {
        this.a = a;
    }

    public static void main(String[] args){
        A a = new A();
        a.setA("a");
        A b = a;
        A c = b;
        a.setA("c");
        System.out.println(a.getA());
        System.out.println(b.getA());
        System.out.println(c.getA());
    }
}

输出

c
c
c

总结:Java中的引用可能和我们平时所认为不太一样,=这个符号不仅仅是赋值
在针对基础类型的时候 ,=就是赋值
在针对非基础类型对象的时候,它就变成了类似于c中的指针的概念了
如有错误,请指出

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值