References and pointers in Java

Source: Internet
Author: User

In some programming scenarios, we often need to record a particular instance location (such as the head node of the linked list that needs to be recorded in the BST-to-bidirectional link list).

In the Java programming process, you need to be aware of the issues that are referenced.

Class ListNode {public int val;public listnode next;public listnode (int _val) {val = _val;}} public class Pointertestinjava {public static void main (string[] args) {ListNode Node1 = new ListNode (1); ListNode Node2 = Null;pointertest (Node1, Node2), if (Node2 = = null) {System.out.println ("Node2 is still null!");} else {Sys Tem.out.println (Node2.val);}} public static void Pointertest (ListNode node1, ListNode node2) {node2 = Node1;}}

The printing results are as follows:

NULL!

Obviously the Pointertest method does not point the Node1 to Node2, making the following changes:

public class Pointertestinjava {public static void main (string[] args) {ListNode Node1 = new ListNode (1); ListNode node2 = new ListNode (2);p ointertest (Node1, Node2), if (Node2.next = = null) {System.out.println ("Node2.left is nul L! ");} else {System.out.println ("Node2.next is" + Node2.next.val);}} public static void Pointertest (ListNode node1, ListNode node2) {node2.next = Node1;}}

Printing results:

Node2.next is 1

At this point, node1.left points to the node2. The reason is that node1.left can be seen as a pointer.

References and pointers in Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.