Java keyword New-----The memory allocation principle of the object __java

Source: Internet
Author: User
Overview of keyword new

"New" is the most commonly used keyword for Java developers, we use new to create objects, use new and instantiate anything we need with the ClassLoader, but you have a deep understanding of what new is doing in the moment of compilation.

It's easy to create objects in Java using the New keyword, and in fact you don't have to think about these things. Need to access a file? Just create a new file instance: "New File" ("Jdbc.properties"), for most Java developers, that's all they need to know, is it simple? But when you use multiple class loaders, the problem is different.

The following is a translation of the Oracle official website article: http://docs.oracle.com/javase/tutorial/java/javaOO/objectcreation.html

As we all know, a class provides a blueprint for an object, and you create an object from a class. The following statement creates an object from the Createobjectdemo program and assigns it to a reference variable:

Point originone = new Point (23, 94);

Rectangle Rectone = new Rectangle (Originone, 100, 200);

Rectangle recttwo = new Rectangle (50, 100);

The first line creates an object of the point class, and the second and third lines create an object that rectangle the rectangular class.

Each of these statements has three parts (discussed in detail):

Declaration declaration: Bold Code is a variable declaration that associates a variable name with an object type.

Instantiating the Instantiating:new keyword is a Java operator that is used to create objects.

Initializes the initialization:new operator, and then calls the constructor to initialize the newly created object. declares a variable to point to an object, that is, a reference

Before that, you know, to declare a variable, you need to write:

Type name;

This tells the compiler that you will use name to reference a type object. With a raw variable, this declaration also retains the appropriate amount of memory for the variable.

You can also declare a reference variable on your own line. For example:

Point Originone;

If you just declare a reference variable like Originone, its value will be pending until an object is actually created and assigned to it. Simply declaring a reference variable without creating an object. For this, you need to use the new operator. Before you use it in your code, you must specify an object for Originone. Otherwise, you will get a compiler error-----null pointer exception.

A variable in this state does not currently refer to any object, as described below (variable name, Originone, a reference that does not point to any object). instantiate an object of a class

The new operator instantiates a class object by allocating memory to the object and returning a reference to that memory. The new operator also invokes the constructor of the object.

Note: "Instantiate an object of a class" means "Create object." When you create an object, you are creating an "instance" of a class, thus "instantiating" the object of a class.

The new operator requires a single, suffix parameter that calls the constructor. The name of the constructor provides the name of the class that needs to be instantiated.

The new operator returns a reference to the object that it creates. This reference is usually assigned to a variable of the appropriate type, such as: Point Originone =new Point (23,94);

A reference returned by the new operator may not need to be assigned to a variable. It can also be used directly in an expression. For example: int height = new Rectangle (). Height; Initializes a class object

This is the point class code

public class Point {

public int x = 0;

public int y = 0;

Constructor

Public point (int a, int b) {

x = A;

y = b;

}

}

This class contains a single constructor. You can identify a constructor because its declaration uses the same name as the class, and it has no return type. The arguments in the point class constructor are two integer arguments, such as code declarations (int a,int b). The following statements provide 94 and 23 as values for these parameters:

Point originone = new Point (23, 94); The result can be described as the following figure

This is the rectangle class, which contains 4 versions of the construction method

public class Rectangle {

public int width = 0;

public int height = 0;

Public point origin;

Four constructors

Public Rectangle () {

Origin = new Point (0, 0);

}

Public Rectangle (point P) {

Origin = P;

}

Public Rectangle (int w, int h) {

Origin = new Point (0, 0);

width = w;

Height = h;

}

Public Rectangle (point p, int w, int h) {

Origin = P;

width = w;

Height = h;

}

A method for moving the rectangle

public void Move (int x, int y) {

origin.x = x;

ORIGIN.Y = y;

}

A method for computing the area of the rectangle

public int Getarea () {

return width * height;

}

}

Each constructor allows you to provide an initial value for the starting value, width, and height of the rectangle, using both the original type and the reference type. If a class has multiple constructors, they must have different signatures. The Java compiler distinguishes constructors based on the number and type of arguments. When the Java compiler encounters the following code, it knows that in the rectangular class, it needs a little controversy, followed by two integer arguments calling the constructor:

Rectangle Rectone = new Rectangle (Originone, 100, 200);

The result can be described as the following figure:

Summary:

The 1.Java keyword new is an operator. The + 、-、 *,/ET operators have the same or similar precedence.

2. The creation of a Java object requires three parts: declaring reference variables, instantiating, initializing object instances.

3. Instantiate: That is, "Create a Java Object"-----Allocate memory and return a reference to that memory.

4. Initialization: is called the construction method, the class of the instance data to assign an initial value.

5.Java Object Memory Layout: Includes object headers and instance data. The following figure:


Object header: It mainly includes the object's own running row metadata, such as hash code, GC generational age, lock status flag, etc. also contains a type pointer to the class metadata indicating the type to which the object belongs.

Instance data: It is a valid information that the object actually stores, including the various types of fields defined in the program code, including those inherited from the parent class and the fields that are owned by it.

In a hotspot virtual machine, the layout of an object in memory can be divided into the object header, the instance data, the alignment padding three parts. Align padding: it is not necessary to exist, just play the role of placeholder.

6.Object obj = new Object ();

The semantics of the

Object obj section will be reflected in the local variables table of the Java stack as a reference type of data. The semantics of this part of the "New Object ()" will be reflected in the Java heap, forming a structured memory that stores all instance data values of type object (Instance data, each instance field in the object). The length of this block of memory is not fixed, depending on the type and the object memory layout that the virtual machine implements (Objects Memory Layout). In addition, you must also include address information in the Java heap that can find the data for this object type, such as Object type, parent class, implemented interface, method, and so on, and those types of data are stored in the method area. Two, memory allocation principle    memory allocation, where to allocate. -------Although the memory allocation of Java objects can use escape analysis techniques and stack allocations, it is undeniable that this is only a secondary means of reducing GC recovery frequency and increasing GC recovery efficiency, so the Java heap area is still the primary region for allocating/storing object instances, no doubt. See http://blog.csdn.net/ljheee/article/details/52226368.

         reference to the description of the Java Virtual Machine Specification (7th edition), the JVM contains three reference types, the type (class type), the type of the array and the interface type ( interface type), where the values of these reference types are dynamically created by class instances, array instances, and derived class instances that implement an interface, how in the JVM is the corresponding object instance created for those types? -------------If you create an object on the Java grammar level, it's simply a simple new keyword, but it's not that simple in the JVM, but the implementation involved in the details is quite complex and a lot of process. Simply put, when the Java syntax layer uses the New keyword to create a Java object, the JVM first checks whether the parameters of the new instruction can navigate to a class's symbolic reference in a constant pool, and then checks whether the class that corresponds to the symbolic reference has successfully experienced loading, parsing, and initialization steps, After the class completes the Mount step, it has fully determined the amount of memory space required to create the object instance, and the JVM will then allocate it in memory to store the generated object instances. As shown in the following illustration:

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.