Distinction between Java and C ++ in terms of basic concepts of surface image objects

Source: Internet
Author: User

Distinction between Java and C ++ in terms of basic concepts of surface image objects
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.

 

II. The most basic difference -- Java is a completely object-oriented language. Objects in object-oriented technology are the ing and embodiment of a specific entity in the real world in computer logic, 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 declaration, but only class definition. 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 collection mechanisms Java provides constructors 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's absence of 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)-There are three inheritance modes for access permissions 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 (II) -- Multi-inheritance the so-called 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.
8. Other differences and conclusions we have discussed above are 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.

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.