Default initialization of C ++ and Java

Source: Internet
Author: User

Default Initialization is a basic problem, but it is often confused, especially when you write Java at the same time and sometimes write C ++. The following is a summary:

In C ++, the basic type is only used as a global variable. For the class type, as long as the class has a non-parametric constructor, no matter whether the class object is a local variable or a global variable, it performs default initialization, even when an object is created through the default initialization mechanism, the class data members of this object will also perform default initialization based on their own non-parametric constructors.

However, if this class does not have a constructor, if this class does not have any constructor, the system will provide a default constructor for this class, theoretically, this constructor will describe that every data member of this class is initialized to 0 of the release type, but this varies with the compiler, so we 'd better not have dependencies, the default no-argument constructor provided by the system is likely to only allocate memory without initialization;

If there are other constructors, the system does not provide the non-argument constructor. At this time, the compiler compilation fails, in actual programming, we can assign default values to all parameters of one of the other constructors to compensate for this situation.

Another scenario where the system does not provide a non-argument constructor is that a class contains uninitialized const or referenced members. In this case, we have no choice but to explicitly initialize the function. Note: initialization cannot be performed inside the class at this time. In fact, there is no static const data member in C ++ who can perform internal class initialization, in addition, data members cannot be initialized within the class, but Java does not have these restrictions.

For this problem, Java has a criterion, that is, when a variable is defined (instead of declaring or not defining only -- of course, there is only one situation: the variable first appears in a function, when no initial value is provided ), it is always initialized to 0 of the appropriate type by default (unless the variable has an initial value in the class definition-again note that this is not feasible in C ++ ). However, unlike C ++, the reference of class-type data members in an object in Java does not use a non-argument constructor for default initialization, but is simply set to null.

Finally, it should be noted that Java does not allow us to provide default values for a function parameter, in C ++, a method that provides a default value for a common constructor parameter to achieve a non-argument constructor is unavailable in Java.

For arrays, the default initialization rules for C ++ are as follows:

For an array of basic types, if it is a global scope, it is initialized to 0 of the appropriate type by default. If it is a local scope, it is not initialized by default.

An array of the class type. If the class used as an array element does not have a no-argument Constructor (one or more other constructor is defined at this time, the system will not provide the default no-argument constructor), the array cannot be declared; if a programmer explicitly provides a no-argument constructor, it will be initialized; if one system provides an implicit non-argument default initialization constructor, it will be initialized in the global scope.

In Java, the array initialization problem of C ++ does not exist. Each element of the array is automatically initialized, And the array element of the Class Object is initialized to null.

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.