Java Handy Sticker 3

Source: Internet
Author: User

Look at the following class:

 class   Cat ( int   ID;    String name;  static  int         SID; Cat (String name) { this . Name = name ;    
ID = sid++; public static void main (string[] args) {cat.sid = 100; Cat Mimi = new Cat ("Mimi" = new Cat ("Pipi"

The allocation of memory is as follows:

1, the first line of the Main method cat.sid = 100

Because the SID is a static variable, regardless of whether the cat class created the object or not, a few objects were created, only a chunk of memory is allocated in data segment, the type is int, and the name is Sid, and the value is 100.

2, the second row new a Cat object, first allocates a piece of memory in the stack, puts the local variable Mimi, and then constructs a cat object in the heap, which has the ID and the name attribute.

String is actually a class, "Mimi" is a string constant that allocates a chunk of memory in data segment, so the Name property is a reference to "Mimi".

3, the third line to build a Cat object Pipi, in the same vein, but at this time the SID becomes 102.

The last memory allocation is as follows:

Java Handy Sticker 3

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.