Memory recovery pits at the end of Java functions

Source: Internet
Author: User

Found the topic of the tree.

The instances created inside the function are recycled if they are only assigned to the parameter.

Function

1  Public Static void Crete (TreeNode node) {2         New TreeNode (111); 3     }

Call

1 NULL ; 2 treeutil.crete (node); 3 System.out.println (node.val);

Abnormal

1 Exception in thread "main" java.lang.NullPointerException2at     Leetcode101.main ( LEETCODE101.JAVA:28)

In principle, we have created an instance of this parameter in the function.

However, this instance will be recycled at the end of the function, so it is still the original null.

What's going to happen? How can I get the result after the operation in the function?

Use the created instance as the return value!

Function

1      Public Static TreeNode Create (int  x) {2         new  TreeNode (x); 3         New TreeNode (x+1); 4         New TreeNode (x+2); 5         return node; 6     }

Call

1         TreeNode node = treeutil.create (1); 2         System.out.println (node.val); 3         System.out.println (node.left.val); 4         System.out.println (Node.right.val);

Output

123

Not only is node's memory still there, his left and right children are still

Memory recovery pits at the end of Java functions

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.