java--object references differ from objects

Source: Internet
Author: User
Preface in Java Learning, understanding objects and referencing objects is the first step in Long March. But now a lot of Java learning Books confuse objects and their references, but in fact they are fundamentally different. To help more Java learners understand better, I write down my understanding of learning. If you have an incomplete or wrong understanding, you are welcome to criticize. Second, analyze the difference between object reference and object for a better analysis, I first set up a demo class for later analysis.
public class demo{//Default construction method public demo{}} Next, we use the Demo class to create an object.

Demo Demo=new demo (); This statement, in fact, includes four actions: 1 to the right " New Demo", is a demo class for templates, in Create a Demo object in heap space
2) at the end of the ()means that after the object is created, Call the Demo class's constructor now, initializes the object that was just generated.
3) to the left of the " Demo Demo"Creates a demo class reference variable that store in stack spaceIn This is the object reference used to point to the demo object.
4) " =operator makes an object reference to the demo object that you just created。 Of course, we can also write this statement:
Demo demo;//Create object references demo=/* object references to Object */new demo ();//Create Object
Third, the relationship between object references and objects 1) Starting from Object reference: An object reference can point to an object
Demo demo;//an object reference demo=new demo ()//an object reference to an object or to an object.
Demo demo;//creates an object reference, but does not point to Object 2: An object can be referenced by an object reference
Demo demo;//Create an object reference Demo=new demo ()//Create an object that is pointed to by an object reference or referenced by multiple object references
Demo demo1,demo2,demo3;//Create multiple object references demo1=new demo (); Demo2=demo1;

demo3=demo2;//creates an object and is pointed to by multiple object references

Original address: https://www.cnblogs.com/java-sky/p/7200085.html

Related Article

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.