I. Introduction
As we all know, C ++ and Java are two mainstream object-oriented languages today. Each person has their own opinions on the advantages and disadvantages of these two languages, the consensus is that C ++ is a compilation-type high-level language while Java is interpreted. Therefore, C ++ is fast and Java is compatible, c ++ is suitable for underlying control while Java is longer than network programming. In any case, it is certain that the two languages will coexist for a long time, which requires us to be proficient in both of them, at least the Framework Structure of the other party should be clearly understood. Because Java is derived from C ++, there are many similarities between the two languages in terms of basic syntax and concepts. However, there are some differences between them that cannot be ignored, this article tries to compare some differences in the basic concepts of object-oriented to discuss the characteristics of the two.
2. The most basic difference -- Java is a completely object-oriented language
Objects in object-oriented technology are ing and embodiment of a specific entity in the real world in computer logic, while classes are collections and abstractions of the same object. All Java programs are composed of classes or class definitions. We should keep this in mind throughout our learning process, because it means that Java is a completely object-oriented language. Everything in Java must be placed into a class. There are no global functions or global data, and there is no such thing as structure, enumeration, or union. Everything is only "class "!
However, C ++ is different. For example, the main method of C ++ is placed out of all classes, and other functions can be defined outside the class. In C ++, some columns such as global variables, structures, enumeration, and Union still exist because of the concept of C. Different people may have different opinions on the difference in this issue. c ++ has the advantage of being flexible and is more conducive to the acceptance of C programmers, because the things set up in C ++ basically do not make any mistakes, they only need to know what is added to C ++, but also because of this, c ++ is a mixture of the two concepts of object-oriented and process-oriented, resulting in many mechanisms that destroy the overall structure of the program while enhancing some functions.
In contrast, the Java language removes the non-object-oriented content reserved in C ++ for compatibility with the C language, which is not very friendly to those who prefer C, the solution to many problems also seems to be simple and complex, but it is in exchange for a rigorous and reliable structure, as well as convenient maintenance and management.
Therefore, the general comparison between the two languages can conclude that C ++ is more flexible and Java is more rigorous.
Iii. Differences between class definition and class method definition
Java does not have independent class declarations and only has class definitions. In terms of defining classes and classes (called member functions in C ++), let's use a typical class definition snippet of C ++ to illustrate the differences between the two:
Class score
{
Score (INT );
};
Score: score (int x)
{
Write down the specific definition of the constructor.
}
This example shows three differences between C ++ and Java:
(1) in Java, class definitions take almost the same form as C ++, but there is no semicolon indicating the end.
(2) All methods in Java are defined in the class subject, but C ++ is not. In Java, we must place the function definition inside the class. This prohibition of method definition outside the class is consistent with the full object-oriented feature of Java.
(3) there is no scope operator ":" in Java. Java uses "." To do everything, but you don't need to consider it, because only one class can define elements. Even the method definitions must be within a class, so there is no need to specify the scope. You can call the static method by using classname. methodname.
Iv. Differences between class and object establishment and Recovery mechanisms
Java provides constructor similar to C ++. If you do not define one, you will get a default constructor. If a non-default constructor is defined, the default constructor will not be automatically defined for us. This is the same as C ++. However, the constructor is not copied in Java, because all the independent variables are passed by reference.
Static initializing is a unique concept of Java. It is opposite to the initialization of each newly created object by constructor. Static initializing is not a method for initializing each class, there is no method name, return value, or parameter list, which is automatically completed when the system loads data to the memory.
On the other hand, in C ++, the release and recycling of objects are implemented through some special operations by programmers, just like creating objects using the new operator, objects can be recycled using the delete operator. However, in Java, to facilitate, simplify programming, and reduce errors, the collection of objects is automatically completed by the system's garbage collection mechanism. Java's garbage collection mechanism is a system background thread, which coexist with the user program and can detect the state of each object in the user program. When it finds that an object can no longer be used by the program, it records it. This unusable object is called memory garbage. When the garbage collection reaches a certain number and the system is not very busy, the garbage collection thread will automatically release the memory of all the garbage objects. In this process, while recycling each garbage object, the system will automatically call the Finalize method to execute it.
The finalize () method is similar to the destructor in C ++, but finalize () is called by the garbage collector, it is only responsible for releasing "resources" (such as open files, sockets, ports, URLs, etc ). To do something in a specific place, You must create a special method and call it. You cannot rely on finalize (). On the other hand, all objects in C ++ are damaged (or "should"), but not all objects in Java are collected as "garbage. Java does not support the Destructor concept, so you must be careful when necessary to create a clearing method. In addition, all cleanup methods must be explicitly called for basic classes and member objects in the class.
V. Differences in overloading-Java has no Operator Overloading
Polymorphism is a special feature of object-oriented programming, and overloading is an important embodiment of it. In C ++, function overloading and operator overloading are supported at the same time. Java is capable of method overloading, but Operator Overloading is not allowed.
Vi. Differences in inheritance (1) -- Access Permissions
There are three inheritance modes in C ++: Public inheritance, private inheritance, and protection inheritance. Public inheritance keeps the access attributes of non-private members in the base class unchanged in the derived class, and protects inheritance from lowering the access attributes of non-private members in the base class in the derived class, private inheritance makes non-private members in the base class become private members in the derived class. In Java, only public inheritance is retained. Inheritance in Java does not change the protection level of basic class members. We cannot specify public, private, or protected inheritance in Java, which is different from C ++. In addition, the Priority Method in the Rule class cannot reduce access to the basic class method. For example, if a member belongs to the public class and we replace it with another method, the method used for replacement must also be public (the compiler will automatically check ).
VII. Differences in inheritance (2) -- about multi-Inheritance
Multi-inheritance means that a subclass can have more than one direct parent class. C ++ supports multiple inheritance in syntax. The format is:
Class derived class name: Access Control keyword 1 base class name 1, access control keyword 2 base class name 2 ,...
To simplify the program structure, Java removes the direct syntax support for multi-inheritance, but uses interfaces to implement the structure of multiple inheritance functions. In this way, there is a significant difference between the two for something that is just designed as an interface, and the extension based on the existing function using the extends keyword. It is not worth using abstract keywords to produce a similar effect, because we cannot create an object belonging to that class. An abstract class can contain abstract methods (though not required to include anything in it), but it can also contain code for specific implementation. Therefore, it is restricted to a single inheritance. Through joint use with interfaces, this solution avoids the need for some mechanisms similar to the C ++ virtual base class. As a result, Java does not have the virtual keyword.
VIII. Differences and conclusions from other aspects
The above is just a comparison between Java and C ++ in terms of closely related basic object-oriented concepts. In addition to these important basic differences, they are more or less different in many places, such as pointer and reference problems, exception mechanisms, and process control problems. We can clearly feel the differences in style between the two languages. Those who love C ++ will say that Java features are poor, while those who love Java will say that C ++ structures are messy. In fact, as a mature object-oriented language, as long as we can grasp its entire system and thoughts and work diligently, we can use them very handy. Writing this article tells you more about what you should pay attention to if you really want to switch from one language to another, rather than asking you to make choices that favor one language.
So no matter which language you choose, remember that the language itself is not competitive, and only the talents who use the language have a higher level.
Other differences:
1. pointer
Java makes it impossible for programmers to find pointers to directly access the memory without pointers, and adds an automatic memory management function to effectively prevent pointer operation errors in the C/C ++ language, such as system crash caused by the wild pointer. But it doesn't mean that Java has no pointer. Pointers are still used inside the virtual machine, but they are not used by outsiders. This is conducive to Java program security.
2. Multi-Inheritance
C ++ supports multiple inheritance, which is a feature of C ++. It allows multiple parent classes to derive a class. Although the multi-inheritance feature is strong, it is complicated to use and can cause a lot of trouble. It is not easy to compile a program to implement it. Java does not support multi-inheritance, but allows a class to inherit multiple interfaces (extends + implement), implementing the C ++ multi-inheritance function, it also avoids the inconvenience caused by multiple inheritance implementation methods in C ++.
3. data types and classes
Java is a fully object-oriented language. All functions and variables must be part of the class. Except for the basic data type, all other objects are class objects, including arrays. Objects combine data and methods and encapsulate them in classes so that each object can implement its own characteristics and behavior. C ++ allows you to define functions and variables as global. In addition, Java removes the structure and union in C/C ++, eliminating unnecessary troubles.
4. Automatic Memory Management
All objects in the Java program are built on the memory stack using the new operator, which is similar to the new operator of C ++. The following statement creates a read-like object and then calls the work method of the object:
Read r = new read ();
R. Work ();
Statement read r = new read (); A read instance is created in the stack structure. Java automatically recycles useless memory and does not need to be deleted by programmers. In C, the program shells must release memory resources and increase the negative throwing of program designers. In Java, when an object is not reused, the useless memory recycler adds tags to it to indicate deletion. Java useless memory recycle programs run in the background in the thread mode and work in idle time.
5. Operator Overloading
Java does not support Operator overloading. Operator Overloading is considered to be a top ten feature of C. Although classes in Java can basically implement such a feature, the convenience of Operator Overloading is still lost. Java does not support Operator Overloading to keep Java as simple as possible.
6. Preprocessing
Java does not support preprocessing. C/C has a well-known pre-processor in the compilation process. The pre-processor provides convenience for developers, but increases the complexity of Ding compilation. The Java virtual machine does not have a Preprocessor, but it provides import statements similar to the functions of the C 10 pre-processor.
7. Java does not support default function parameters, but C 10 supports
In C, the code is organized in the function, and the function can access the global variables of the program. Class is added to Class C 10 and class algorithms are provided. This algorithm is a function connected to the class. Class C 10 methods are very similar to Java class methods. However, since Class C 10 still supports Class C, therefore, it is not possible to prevent C 10 developers from using functions. The mixed use of result functions and methods makes the program messy.
Java does not have a function. As a language that is more pure than the top ten object-oriented languages of C, Java forces developers to include all routines in the class. In fact, routine implementation with methods can motivate developers to better organize codes.
8 string
C and C 10 do not support string variables. The null Terminator is used in C and C 10 programs to indicate the end of the string. in Java, the string is a Class Object (strinr and stringbuffer) these class objects are the core of the Java language. Using class objects to implement strings has the following advantages:
(1) the method for establishing strings and accessing string elements in the entire system is consistent;
(2) The J3 Yang string class is defined as part of the Java language, rather than as an extension of the plus;
(3) the Java string runtime check is empty, which helps to eliminate some running errors;
(4) You can use "10" to connect strings.
9 "GOTO statement
The "terrible" GOTO statement is a "relic" of C and C ++. It is a legal part of the language technology. referencing a GOTO statement causes confusion in the program structure and is hard to understand, the Goto substatement must be used for unconditional transfer of subprograms and multi-structure branch technology. For a wide range of reasons, Java does not provide a GOTO statement. Although it specifies a goto as a keyword, it does not support its use, making the program simple and easy to read.
L0. type conversion
Implicit conversions of Data Types sometimes occur in the 10th of C and C, which involves automatic forced conversions. For example, you can assign a floating point value to an integer variable in December 10, c, and remove its ending number. Java does not support automatic forced type conversion in the top 10 C program. If necessary, the program must explicitly perform forced type conversion.
11. Exceptions
The exception mechanism in Java is used to capture exception events and enhance the system fault tolerance capability.
Try {// code that may generate exceptions
} Catch (exceptiontype name ){
// Process
}
Predictiontype indicates the exception type. C ++ does not have such a convenient mechanism.