Default initialization problems for C + + and Java

Source: Internet
Author: User
Tags class definition constructor

Default initialization This is a fundamental issue, but it is often somewhat confusing, especially when you write Java at the same time and sometimes write C + +, summed up as follows:

The base type in C + + is only performed as a global variable, and for a class type, whenever the class has an parameterless constructor, whether the object of the class is a local or global variable, it initializes the default, even when an object is created by default initialization mechanism. The class-type data members of this object are also initialized by default based on their own parameterless constructors.

But if the class does not have a parameterless constructor, so if this class does not have any constructors, the system will provide a default parameterless constructor for the class, which in theory would say that each data member of the class is initialized to 0 of the release type, but this is different from the compiler, and we'd better not have to rely on The default parameterless constructor provided by the system is entirely likely to be memory-allocated without initialization;

If there are other constructors, then the system does not provide an parameterless constructor, at which time the compiler does not pass, and in practical programming we can make up for the default assignment of all the parameters of one of the other constructors.

Another case where the system does not provide an parameterless constructor is that a class contains an uninitialized const or reference member, in which case we have no choice but to initialize the function explicitly. Note that it is not possible to initialize within the class at this time, in fact, in C + +, there is no static const type of data members can be initialized within the class, in addition to data members cannot initialize within the class, but Java does not have these restrictions.

For this problem, Java has a rule that when a variable is defined (rather than just declaring undefined--of course, there is only one: A variable that first appears inside a function and does not provide an initial value), it is always default initialized to the appropriate type of 0 ( Unless the variable already has an initial value in the class definition-note again that this is not feasible in C + +. Unlike C + +, however, a reference to a class type data member in one of the Java objects does not use the parameterless constructor for the default initialization, but simply null.

Finally, it should be noted that Java does not allow us to provide a default value for the parameters of a function, because we can no longer use it in Java in C + + by supplying a default value to a regular constructor parameter to achieve the parameterless constructor.

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

An array of base types that are initialized by default to the appropriate type 0 if the global scope is not initialized by default if the local scope is the domain.

An array of class types, if the class that is an array element does not have an parameterless constructor (one or more other constructors are defined at this time), the system also does not provide a default parameterless constructor, the array cannot be declared, and if a programmer explicitly provides a parameterless constructor, it is initialized; If there is a system-supplied implicit parameterless default initialization constructor, it will be initialized on the global scope.

In Java, the problem with the array initialization of C + + does not exist, the elements of the array are automatically initialized, and the array elements of the class type object are 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.