Object and reference, Object Reference

Source: Internet
Author: User

Object and reference, Object Reference
Objects and references

Let's look at this example first:

Int [] a = {5, 5}; int [] B = a; B [1] = 45;

The output value of array a is as follows: 5, 45, 5

This is because a and B point to the same object {5, 5}. both a and B are referred to as references to this object, that is, a and B operate on the same object.

 

Empty object
Int [] a = {5, 5}; int [] B = a; a = null;

Set a to null, which means that a will not point to any object.


As a resultNull Pointer Error NullPointerException
As follows:

Int [] a = {11,15, 14}; a = null; a [0] = 12; // Error

If a is set to null, it will not point to any object, and it will not be able to access a [0].

 

The referenced object is lost.
Int [] a = {8, 8}; a = new int [4];

A originally pointed to the object {8, 8}, and then a was set to point to the object int [4], so {8, 8} was not referenced, that is, if the referenced object is lost, it will be recycled and deleted by the java recycle mechanism.

 

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.