The distinction between Java and C + + in the basic concept of surface object

Source: Internet
Author: User

First, Introduction

As we all know, C + + and Java are today's two mainstream object-oriented language, for the pros and cons of the two languages, everyone has their own different views, the more consistent view is that C + + is a compiled high-level language and Java is interpreted, so C + + fast and Java compatibility Good, C + + Suitable for bottom control while Java is longer than network programming. In any case, it is certain that the two languages will coexist for a considerable period of time, which requires us to be proficient in both, and to have at least a clear grasp of the framework structure of the other. Since Java is derived from C + +, there are many similarities between the two languages in terms of basic grammar and concept, but there are also some noticeable differences between them, this paper attempts to talk about the characteristics of both by comparing some differences in the basic concepts of object-oriented.


second, the most basic difference--java is a complete object-oriented languageObject in object-oriented technology is the mapping and embodiment of a specific entity in the real world in the computer logic, and the class is the collection and abstraction of the same object. All Java programs are made up of classes or class definitions. This should be kept in mind throughout our learning process, because it means that Java is a complete object-oriented language. Everything in Java has to be placed into a class. There is no global function, no global data, no such thing as structure, enumeration, or union.
However, C + + is different, such as C + + 's Main method is placed outside of all classes, in addition to other functions can be defined outside the class. In C + +, some of the concepts of global variables, structs, enumerations, unions, and so forth that originate from C still exist. For the difference in this issue, different people will have different views, C + + 's advantage is flexible, and more conducive to C programmers to accept, because the things set up in C C + + basically do not have errors, they only need to understand the C + + more things can be, but also because of this, C + + A mixture of object-oriented and process-oriented dual concepts, resulting in a number of mechanisms to strengthen a certain part of the function while destroying the overall structure of the program.
Compared with this, the Java language to remove the C + + in order to be compatible with the non-object-oriented content, for the C-habit of the people are not very friendly, many problems in the processing also appear to be somewhat abandoned, but it in exchange for a rigorous and reliable structure, and maintenance and management of the convenience.

Therefore, the overall comparison between the two languages can be concluded that C + + more flexible and more rigorous java.


differences in definitions of class definitions and class methodsThere is no separate class declaration in Java, only the class definition. On defining the methods of classes and classes (called member functions in C + +), let's use a fragment of a typical C + + class definition to illustrate the difference:
Class score
{
Score (int);
};
Score::score (int x)
{
Write down the specific definition of the constructor function
}
This example reflects the three differences between C + + and Java:
(1) In Java, the class definition takes almost the same form as C + +, except that there is no sign ending semicolon.
(2) All methods in Java are defined in the body of the class and C + + is not. In Java, we have to place the definition of a function inside the class, a prohibition that is consistent with the definition of the method outside of the class and the full object-oriented nature of Java.

(3) There is no scope scope operator "::" in Java. Java uses "." Do all the things, but don't worry about it, because you can define elements in only one class. Even those method definitions must be inside a class, so it is not necessary to specify scope scopes at all. The call to the static method is also possible by using Classname.methodname ().


Iv. Differences between the establishment of classes and objects and the mechanism of recoveryJava provides a constructor similar to C + +. If you do not define one yourself, you will get a default constructor. If a non-default constructor is defined, the default constructor is not automatically defined for us. This is the same as C + +. However, there is no copy constructor in Java because all arguments are passed by reference.
Static initializers are a unique concept of Java, and the static initializer initializes each class as opposed to the constructor for each new object initialization, which is not a method, no method name, return value, and parameter list, which is automatically completed when the system is loaded into memory.
On the other hand, in C + +, the release and recycling of objects is accomplished by a programmer doing something special, like creating an object with the new operator, and using the delete operator to reclaim an object. In the Java language, for ease of programming and error reduction, object recycling is done automatically by the system's garbage collection mechanism. Java's garbage collection mechanism is a system background thread that coexists with the user's program to detect the state of objects in the user's program. When it finds an object that cannot continue to be exploited by the program, the object is recorded, and the reusable object is called memory garbage. When the garbage reaches a certain number and the system is not very busy, the garbage collection thread automatically completes the memory release of all the garbage objects, and in this process, the finalizer (Finalize) method that executes it is automatically called when each garbage object is reclaimed.

The Finalize () method has a similar place to the destructor (destructor) in C + +, but finalize () is called by the garbage collector and is responsible for releasing "resources" (such as open files, sockets, ports, URLs, and so on). To do something in a specific location, you must create a special method and call it, and you cannot rely on finalize (). On the other hand, all objects in C + + will be destroyed (or "should"), but not all objects in Java will be collected as "garbage". Because Java does not support the concept of destructors, you must carefully create a purge method when necessary. And for the underlying classes and member objects within the class, you need to explicitly call all the cleanup methods.


v. Differences in overloading--java no operator overloadingPolymorphism is a special feature of object-oriented programming, and overloading is an important embodiment of it. In C + +, both function overloading and operator overloading are supported, while Java has the capability of method overloading, but operator overloading is not allowed.
vi. differences in Inheritance (i)--about access rightsThere are three inheritance modes in C + +-public inheritance, private inheritance, and protection inheritance. Where public inheritance keeps non-private members of the base class in the derived class, the Access property remains the same, and protection inheritance causes non-private members in the base class to drop one level in the Access property of the derived class, while private inheritance makes the non-private members in the base class A private member in the derived class. In Java, only public inheritance is preserved, and inheritance in Java does not alter the level of protection of the underlying class members. We cannot specify public,private or protected inheritance in Java, which is different from C + +. In addition, the precedence method in a derived class does not reduce access to the underlying class method. For example, if a member is public in the underlying class and we replace it with another method, then the method used for substitution must also be public (the compiler will check it automatically).
vii. differences in Inheritance (II.)--on multi-inheritanceThe so-called multiple inheritance, refers to a subclass can have more than one direct parent class. C + + directly supports multiple inheritance in syntax, in the form of:
Class Derived classes name: Access Control keyword 1 base class name 1, access control keyword 2 base class Name 2, ...
Java, for the sake of simplifying the structure of the program, cancels the syntax of the direct support for multiple inheritance, but uses the interface to implement the structure of multiple inheritance functions. In this way, there is a noticeable difference between what is designed to be an interface, and the expansion of the extends keyword on the basis of existing functionality. It's not worth having a similar effect with the abstract keyword, because we can't create an object that belongs to that class. An abstract (abstract) class can contain an abstract method (although it is not required to contain anything in it), but it can also contain code for specific implementations. Therefore, it is limited to a single inheritance. This scheme avoids the need for some mechanism similar to the C + + virtual base class by using it in conjunction with the interface. As a result, there is no virtual keyword in java.
Viii. Other aspects of the difference and conclusionThe above we introduce, just Java and C + + in the basic concept of object-oriented comparison of some of the more closely related aspects, in addition to these important basic differences, they are in many places are more or less different, such as pointer and reference problems, abnormal mechanism problems, process control problems and so on. We can clearly feel the difference in style between the two languages in various aspects. People who love C + + will say that Java is poor, Java-like people will say C + + structure confusion, in fact, as a relatively mature object-oriented language, regardless of which, as long as we can master its entire system and ideas and practice, can be very handy to use. Writing this article is more about telling you what you should be aware of if you really want to move from one language to another, rather than encouraging you to make a preference for a particular language.
So no matter which language you choose, remember that the language itself does not compete, only the people who use the language.

The distinction between Java and C + + in the basic concept of surface object

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.