Comparison between Java and C ++ variable Initialization
Java tries its best to ensure that all variables can be properly initialized before use
① Function/method local variable Initialization
In C/C ++, variable initialization depends on the programmer's consciousness. For local function variables, the compiler does not assign the default initial value to the basic type. New users often use Uninitialized pointers to access the memory, causing program crash. For class objects, the compiler initializes the objects using the default class constructor. In Java, Java uses compile-time errors for local variables of methods to ensure proper initialization of variables before use.
void f(){ int i ; i ++ ; //Error- - i not initialized}
Although the java compiler can also assign an initial value to the local variables of the method, it is more likely that the local variables are not initialized than the programmer's negligence. Using the default value will mask this error.
② Class data member Initialization
C ++ programmers may be unaccustomed to accessing java classes. Data members of java classes can initialize them at definition:
public class InitialValues{boolean bool = true;char ch = ‘x’;int i = 999;double d = 3.14;Depth d = new Depth();}
This method is called specified initialization in java. Before the initialization is specified, the compiler will perform default initialization for these data members. In fact, the allocated object memory is set to zero.
// The default initialization public class InitialValues {boolean t; // flase char c; // [] short s; // 0 byte B; // 0 int I; // 0 long l; // 0 float f; // 0.0 double d; // 0.0}
When a reference is defined in an object and is not initialized, the default value is null. The default initialization implementation is that when an object is created (new), the storage space will be cleared after sufficient space is allocated to the object on the stack, in this way, all data members of the basic type are automatically set to the default value. The specified Initialization is executed only after the default initialization action. That is to say, the following I has gone through the process of being initialized to 0 and then assigned a value of 999.
public class InitialValues{int i = 999;}
Java can also use constructor for initialization, but the initialization of constructor cannot prevent the specified initialization and default initialization, And the constructor initialization will always be executed after them. To sum up, the initialization process of data members in java is:
- ① Initialize by default
- ② Initialize the definition (specify initialization)
- ③ Constructor Initialization
C ++ prohibits specified initialization when defining data members, and C ++ does not have default initialization. Someone may ask, isn't the following code initialized by default?
Class Test {public: int I; double B; char ch ;}; int main () {Test * t = new Test (); cout <t-> B; // output 0 cout <t-> I; // output 0 cout <t-> ch; // output [] return 0 ;}
This is actually the initialization of the constructor implemented by the default constructor of C ++. When a class has no constructor, the compiler declares the class and implements a default constructor. The default constructor initializes the data member to the default value. Therefore, the initial values of C ++ data members can only be dependent on:
- Member initialization list
- Constructor
The member initialization list is similar to the specified initialization in java. It is also used to initialize data members before they enter the constructor body. Java and C ++ are consistent in the initialization sequence of data members. The defined sequence determines the initialization sequence.
Static member Initialization
Static local variables, such as static data members, are not allowed in java. Static data members are instantiated only once when the object is created for the first time. For example:
Class StaticTest {int _ a; StaticTest (int a) {_ a = a; System. out. println ("StaticTest (" + a + ")") ;}} class Test {static StaticTest st1 = new StaticTest (1); StaticTest nonSt = new StaticTest (0 ); static StaticTest st2 = new StaticTest (2);} public class Main {public static void main (String [] args) throws Throwable {Test t = new Test (); // st1 and st2 will not be instantiated until this time} // The program outputs StaticTest (1) StaticTest (2) StaticTest (0)
From the output, we can see that in Java, the initialization sequence is as follows: Initialize static data members first, and then initialize non-static data members.
In C ++, static data members must be initialized outside the class. For more information about the static functions of C ++, see.
Back to the beginning.
Java tries its best to ensure that all variables can be properly initialized before use (Java programming ideas). For details, see pdf download.
In Java, common variables are initialized by default by means of compilation errors and member variables, and all variables can be initialized before use, it is more secure than C ++.