I. Definition and default initialization of Variables
When variables are declared in C ++, most of them are defined (that is, memory is allocated). The special cases include:
1) extent int X, which declares only X and does not allocate memory to X.
2) function declaration (that is, the Declaration made before the function is called, which is not defined at this time ).
3) class declaration.
Variables in methods in Java are not initialized during declaration. Some people think that they will be initialized to null, but they are not. If they are not initialized, the compiler will report an error until they are assigned or called.
The member variable is initialized during the Declaration and initialized to a default value. For example, if the integer is 0, the object is initialized to null.
The local data in C ++ is initialized by default only when the global data is used.
Ii. Static
The meaning of static in C ++ is much wider than that in Java. The static variable itself has a "shared" attribute, and its "user" permissions are readable and writable. If one of them changes its value, other "users" can only accept this change. For C ++:
1) Global static variables or local static variables can be used by "users" in their corresponding scopes;
2) static member variables and static member functions in the class can be accessed by class names or objects (if permitted), and their static member variables can only be initialized outside the class:
Variable_type classname: variable_name = value.
For Java, Because Java strictly abides by the idea of OOP, static is only reflected in the member variables and methods of the class. Usage is the same as 2), but its initialization can be completed within the class.
Iii. Scope
C ++ allows a block sub-block to define the same variable name as this block, and the sub-block variables completely overwrite the variables of the parent block. Java is not allowed.
Iv. Operator Overloading
Java does not provide the operator overload function as C ++ does. Therefore, using "=" in Java to judge that the string is equal is no longer useful ("=" in this case only indicates that the position is the same, although strings with the same position must be the same, strings with the same value may not be in the same position), you need to use the equals (string Str) method ).
5. Powerful "for each" Loop
Java has such a for loop statement:
For (element: Collection)
Collection is a set that implements the interable interface. In C ++, this statement is not used.
Vi. object Value assignment reference
The test. Java file contains the following: Code :
Public class test
{
Public test (INT value)
{
This. value = value;
}
Public void print ()
{
System. Out. println (value );
}
Public void setvalue (INT value)
{
This. value = value;
}
Private int value;
Public static void main (string ARGs [])
{
Test test1 = new test (1 );
Test Test2 = test1;
Test2.setvalue (2 );
Test1.print ();
}
}
The output is 2. It can be seen that the "=" operation between objects in Java is equal to that in C ++ "&" (reference). The "=" Operation of C ++ objects is actually a copy operation, it is equivalent to the clone (object other) method in Java. For arrays, Java also treats them as objects, such as the following statement:
Int [] intarr = {1, 2, 3 };
Int [] intarr2 = intarr;
Intarr2 [0] = 3;
System. Out. println (intarr [0]);
The output value is 3.
7. Default dynamic binding
In Java, subclasses will automatically overwrite the methods of the parent class with the same name, instead of declaring the parent class method as vitual type like C ++. If you do not want to overwrite the subclass, use final. That is to say, C ++ does not overwrite by default. to overwrite the data, you need to manually describe it. Java overwrites the data by default.
VIII. Multiple inheritance and interfaces
Multiple
Inherit) can easily lead to ambiguity and repeated inheritance, far more complex than a single inheritance. Java claims that it does not support multiple inheritance, but it cleverly utilizes the interface concept,
Multi-inheritance is implemented. In fact, the virtual base class without member variables is often used in multi-inheritance in C ++ (Abstract
Class) This is actually equivalent to the Java interface. Visually, interfaces in Java are more like "rules ". For example, comparable
Interface. All the classes that need to perform object comparison operations must comply with the comparable rules, that is, the compareto method must be implemented. In multi-inheritance, the parent class is not only used to define rules.
Yes. It also provides some data and methods.
9. Nested classes in C ++ and internal classes in Java
In C ++, nested classes are equivalent to "Local classes", which are equivalent to local variables and can resolve name conflicts and access control. The internal classes in Java are far more complex than those in C ++. Because Java does not have the concept of variable overwrite (see Article 3 ), therefore, the original design of internal classes is very different from that of C ++ Nested classes. Its functions are as follows:
1) The internal class can easily access any member of its external class (like the method of the external class ).
2) Internal classes can be modified using private. At this time, only external classes can create their instances.
(To be continued)
10. Call value passing through Java function parameters. If the parameter is of the basic type, the parameter itself is immutable. If the parameter is an object reference, the reference cannot point to a new object.
There are three principles:
1. for parameters of the basic type, the function cannot change its value.
2. For object reference parameters, the function cannot reference the target
3. For object reference parameters, the function can change the state of the referenced object.
Therefore, the swap (T & T1, T & T2) (or swap (T * pt1, T * pt2) function is feasible in C ++, it cannot be done in C ++. To exchange the values of two variables, unless they are put into a class and then exchanged using the third method above.
As follows:
Class Test
{
Public int I0 = 0;
Public int I1 = 1;
}
Swap (test T)
{
Int TEM = T. I0;
T. I0 = T. I1;
T. I1 = TEM;
}
I. Definition and default initialization of Variables
When variables are declared in C ++, most of them are defined (that is, memory is allocated). The special cases include:
1) extent int X, which declares only X and does not allocate memory to X.
2) function declaration (that is, the Declaration made before the function is called, which is not defined at this time ).
3) class declaration.
Variables in methods in Java are not initialized during declaration. Some people think that they will be initialized to null, but they are not. If they are not initialized, the compiler will report an error until they are assigned or called.
The member variable is initialized during the Declaration and initialized to a default value. For example, if the integer is 0, the object is initialized to null.
The local data in C ++ is initialized by default only when the global data is used.
Ii. Static
The meaning of static in C ++ is much wider than that in Java. The static variable itself has a "shared" attribute, and its "user" permissions are readable and writable. If one of them changes its value, other "users" can only accept this change. For C ++:
1) Global static variables or local static variables can be used by "users" in their corresponding scopes;
2) static member variables and static member functions in the class can be accessed by class names or objects (if permitted), and their static member variables can only be initialized outside the class:
Variable_type classname: variable_name = value.
For Java, Because Java strictly abides by the idea of OOP, static is only reflected in the member variables and methods of the class. Usage is the same as 2), but its initialization can be completed within the class.
Iii. Scope
C ++ allows a block sub-block to define the same variable name as this block, and the sub-block variables completely overwrite the variables of the parent block. Java is not allowed.
Iv. Operator Overloading
Java does not provide the operator overload function as C ++ does. Therefore, using "=" in Java to judge that the string is equal is no longer useful ("=" in this case only indicates that the position is the same, although strings with the same position must be the same, strings with the same value may not be in the same position), you need to use the equals (string Str) method ).
5. Powerful "for each" Loop
Java has such a for loop statement:
For (element: Collection)
Collection is a set that implements the interable interface. In C ++, this statement is not used.
Vi. object Value assignment reference
Here is the most easily overlooked and the most serious difference. The test. Java file contains the following code:
Public class test
{
Public test (INT value)
{
This. value = value;
}
Public void print ()
{
System. Out. println (value );
}
Public void setvalue (INT value)
{
This. value = value;
}
Private int value;
Public static void main (string ARGs [])
{
Test test1 = new test (1 );
Test Test2 = test1;
Test2.setvalue (2 );
Test1.print ();
}
}
The output is 2. It can be seen that the "=" operation between objects in Java is equal to that in C ++ "&" (reference). The "=" Operation of C ++ objects is actually a copy operation, it is equivalent to the clone (object other) method in Java. For arrays, Java also treats them as objects, such as the following statement:
Int [] intarr = {1, 2, 3 };
Int [] intarr2 = intarr;
Intarr2 [0] = 3;
System. Out. println (intarr [0]);
The output value is 3.
7. Default dynamic binding
In Java, subclasses will automatically overwrite the methods of the parent class with the same name, instead of declaring the parent class method as vitual type like C ++. If you do not want to overwrite the subclass, use final. That is to say, C ++ does not overwrite by default. to overwrite the data, you need to manually describe it. Java overwrites the data by default.
VIII. Multiple inheritance and interfaces
Multiple
Inherit) can easily lead to ambiguity and repeated inheritance, far more complex than a single inheritance. Java claims that it does not support multiple inheritance, but it cleverly utilizes the interface concept,
Multi-inheritance is implemented. In fact, the virtual base class without member variables is often used in multi-inheritance in C ++ (Abstract
Class) This is actually equivalent to the Java interface. Visually, interfaces in Java are more like "rules ". For example, comparable
Interface. All the classes that need to perform object comparison operations must comply with the comparable rules, that is, the compareto method must be implemented. In multi-inheritance, the parent class is not only used to define rules.
Yes. It also provides some data and methods.
9. Nested classes in C ++ and internal classes in Java
In C ++, nested classes are equivalent to "Local classes", which are equivalent to local variables and can resolve name conflicts and access control. The internal classes in Java are far more complex than those in C ++. Because Java does not have the concept of variable overwrite (see Article 3 ), therefore, the original design of internal classes is very different from that of C ++ Nested classes. Its functions are as follows:
1) The internal class can easily access any member of its external class (like the method of the external class ).
2) Internal classes can be modified using private. At this time, only external classes can create their instances.
(To be continued)
10. Call value passing through Java function parameters. If the parameter is of the basic type, the parameter itself is immutable. If the parameter is an object reference, the reference cannot point to a new object.
There are three principles:
1. for parameters of the basic type, the function cannot change its value.
2. For object reference parameters, the function cannot reference the target
3. For object reference parameters, the function can change the state of the referenced object.
Therefore, the swap (T & T1, T & T2) (or swap (T * pt1, T * pt2) function is feasible in C ++, it cannot be done in C ++. To exchange the values of two variables, unless they are put into a class and then exchanged using the third method above.
As follows:
Class Test
{
Public int I0 = 0;
Public int I1 = 1;
}
Swap (test T)
{
Int TEM = T. I0;
T. I0 = T. I1;
T. I1 = TEM;
}