Java Object-oriented _ Object memory analysis-value passing and reference passing

Source: Internet
Author: User

Object Memory analysis, I have been learning more vague, today time to tidy up, hope to clarify.

First of all, where does the memory analysis come from, which is more obscure to those of us who are just touching Java, starting with the new keyword.

The new keyword means that creating an object or instantiating an object or applying a memory space requires memory to store the data, so the concept of memory comes out. For example: Define a class person, attribute: char a;int sge; the memory space for creating an object request is the sum of the bytes of all the attributes, which is 6 bytes.

Detail the structure of the object in memory:

Person P=new person (); Person P1=new person ();

Stack memory heap

P

Name=null

Age=0

P1
a=10 (local variable)

No assignment defaults to NULL. The stack memory is stored as a class object or as a local variable (the base data type). Local variables: Acting on a method;

Global variables: Acting on the entire class

If P1=p;p and P1 point to the same heap of memory, the memory that P1 originally points to is not referenced and is garbage collected.

Value is passed in the stack memory, and the value is copied directly.

The reference pass is passed through the address in the heap memory. Like P and P1 addresses, they point to the same piece of memory.

Almost, the memory analysis is summed up here.

Java Object-oriented _ Object memory analysis-value passing and reference passing

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.