In the case of a reference type memory

There is a class already declared good, class Car {

  String name;

}

This class has a name attribute, an object to instantiate the following, assignment of its properties,

Car car1=new Car();

car1.name = "modern";

In memory of the whole process is kind of how it? Our first memory divided into three regions, namely, stack memory, the heap memory storage area.

Class loading process, the JVM first open up a space in the memory, a younger brother called JVM ClassLoader class loader to load placed on your hard disk files to the class

Storage area, of course, there is in fact not really copy the class file on the hard disk into memory, after all, a great actual file, the computer's memory is limited, it is fully put in how much memory is not enough.

So here are just (there are subdivided into three storage areas, a constant buffer zone method (refers to classes, methods, relying on class inside the Well), a static element zone (static)) in the method area storage area established a mapping relationship,

In the method area we will have a template class Car with exactly the same hard disk.

Car car1=new Car();

Car car1 this last part, declares a reference type car1 the stack memory, if you want new Car (), is the method by Car template in the establishment of a zone of memory space in the heap that new Car,

By '=' assigned to car1, where the assignment is given car1 new Car in the heap memory address.

car1.name = "modern"; meaning of this sentence is to be found through the stack memory heap memory address car1 name, and assign to it (note that object properties of a new beginning has initial default value)

If you declare a

Car car2=new Car();

car2.name = "Bentley";

So exactly the same with the previous process, the heap memory space has opened up a new car2, the two affect each other.

Car car1 statement only if, it is not new

Car car1=car2;

So what's printed name should be?

car1 if not new, you can not open in the heap memory space, i.e. instantiated object does not exist.

The car2 assigned to car1, the actual address car2 gave car1, then the value of car1.name if it becomes like a car2.name. Both point to a Bentley this address.

 

Guess you like

Origin www.cnblogs.com/hebiao/p/11576223.html
Recommended