Thinking --java value passed by reference and pass a piece of code caused

Today, the junior partner asked me a piece of code:

  public static ListNode create(int[] array) {
        if (array == null || array.length == 0)
            return null;
        ListNode head = new ListNode(0);
        ListNode node = head;
        for (int i = 0; i < array.length; i++) {
            ListNode temp = new ListNode(array[i]);
            node.next = temp;
            node = temp;
            System.out.println(node.val);
        }
        return head.next;
    }

Why the head points to the last method a linked list of nodes, but in the main method is directed first head node.

I tested



public class Main {

    public static class ListNode{
        int val;
        ListNode next; //下一个链表对象
        ListNode(int x){val=x;}  //赋给结点的值
    }

    public static ListNode create(int[] array) {
        if (array == null || array.length == 0)
            return null;
        ListNode head = new ListNode(0);  //预指针,其下一个结点指向真正的头结点
        ListNode node = head;
        for (int i = 0; i < array.length; i++) {
            ListNode temp = new ListNode(array[i]);
            node.next = temp;
            node = temp;
            System.out.println(node.val);
        }
        return head.next;
    }

    public static void main(String[] args)
    {
        int[] array={1,2,3,4,5,6,7,8,9};
        ListNode a=create(array);
        System.out.println("---------"+a.val);
    }
}

The discovery of something a.val output is 1.

Later, after a wave of discussions, access to a lot of information about the conclusions drawn.

This is because ListNode a = create (array); is initialized, the initial value of 1 assigned to the head.next a. And then execute the create method. However, due to the transfer java reference, although the last value of the head 9 becomes method, but a final value assigned to its initial value 1 remains.

Value is passed:
when the method is called, the arguments by the parameter copy its contents incoming internal method, then the contents of the received parameter is a copy of the argument value, so the parameter in the operation of the method of any They are just a copy of this operation does not affect the content of the original value.

Passed by reference:
reference is pointing to the real content of the address value, when the method is called, arguments of the address by the process is passed to the corresponding parameter called in vivo, formal and actual parameters point to a memory address of the parameter of the true value of the operation will affect.

JVM memory model, the method is the presence of a virtual stack, a thread exclusive a stack, and heap and method area is shared by all threads. JAVA pass into the function when the reference, a reference to the copied argument a reference point to the same object, not a reference to the same operation. As shown below:

Overall it is only when changing the value of the variable itself occurred, the value will change.

Published 47 original articles · won praise 6 · views 20000 +

Guess you like

Origin blog.csdn.net/floraruo/article/details/104045290