The difference between Java and C + +

Source: Internet
Author: User

1. The pointer Java language allows programmers to not find pointers to direct access to memory, and adds automatic memory management capabilities to effectively prevent pointer manipulation errors in C + + languages, such as system crashes caused by wild pointers. But it is not that Java does not have pointers, the virtual machine inside or use a pointer, but outsiders are not allowed to use it. This facilitates the security of Java programs.


2. Multiple inheritance C + + supports multiple inheritance, a feature of C + + that allows a class to be derived from a multi-parent class. Although multiple inheritance is powerful, it is complex to use and can cause a lot of trouble, and it is not easy for the compiler to implement it. Java does not support multiple inheritance, but allows a class to inherit multiple interfaces (Extends+implement), realizes the function of multiple inheritance of C + +, and avoids the inconvenience caused by multiple inheritance implementations in C + +.


3. Data types and Classes Java is a fully object-oriented language, and all functions and variables must be part of the class. In addition to the basic data types, the rest are used as class objects, including arrays. Objects combine data and methods to encapsulate them in a class so that each object can implement its own characteristics and behavior. C + + allows functions and variables to be defined as global. In addition, the structure and union in C + + is removed in Java, eliminating unnecessary hassles.


4. Automatic memory management all objects in a Java program are built on the memory stack with the new operator, which resembles the new operator of C + +. The following statement consists of an object that establishes a class read, and then calls the work method of the object:

12 Read r=newRead();r.work();

The statement read R=new read (), and a read instance was created on the stack structure. Java automates garbage collection operations without the need for a programmer to delete them. In C + +, the memory resource must be freed by the program shell, and the program designer's negative throw is added. In Java, when an object is not reused, the garbage collector adds a label to it to delete it. Useless memory recycling programs in Java run in the background in a threaded fashion, using idle time to work.


5. operator overloading does not support operator overloading. Operator overloading is considered to be a prominent feature of C + +, although in Java the class can generally implement such a function, but the convenience of operator overloading is still missing a lot. The Java language does not support operator overloading in order to keep the Java language as simple as possible.


6. Preprocessing functionality Java does not support preprocessing. C + + has a pre-coding phase in the compilation process, known as a pre-processor. The preprocessor provides convenience for developers, but adds complexity to the compilation. The Java Virtual machine does not have a preprocessor, but it provides an introduction statement (import) similar to the functionality of a C + + preprocessor.


7. Java does not support default function parameters, while C + + supports C, where code is organized in functions, functions can access the program's global variables. C + + Adds classes, provides class algorithms, which are functions connected to classes, C + + class methods are similar to Java class methods +, however, since C + + still supports C, it is not possible to prevent C + + developers from using functions, resulting in mixed use of functions and methods to make the program more chaotic. Java has no functions that are not included in the class, and as a more pure object-oriented language than C + +, Java forces developers to include all the routines in the class, and in fact, implementing routines in a way that motivates developers to better organize coding.


8. strings C and C + + do not support string variables, and the use of a null terminator in C and C + + programs represents the end of a string, and in Java strings are implemented using class objects (STRINR and StringBuffer), which are the core of the Java language , implementing a string with a class object has several advantages:

(1) The method of establishing strings and accessing string elements is consistent throughout the system;

(2) The Java string class is defined as part of the Java language, not as an additional extension;

(3) Java string execution run-time check-out to help troubleshoot some runtime errors;

(4) The string can be connected with "+".


9. goto statement "Terrible" goto statement is C and C + + "relic", it is the technical legal part of the language, referring to the GOTO statement caused the program structure confusion, difficult to understand, goto sentence to be used for unconditional transfer subroutine and multi-structure branch technology. Given the wide justification, Java does not provide a goto statement, which, while specifying Goto as the keyword, does not support its use, making the program simple and readable.


l0. Type conversions sometimes include implicit conversions of data types in C and C + +, which involves automatic coercion of type conversions. For example, in C + +, you can assign a floating-point value to an integer variable and remove its mantissa. Java does not support automatic coercion of type conversions in C + + and, if required, must be explicitly enforced by the program.


11. exception mechanisms in abnormal Java are used to capture exception events and enhance system fault tolerance

The difference between Java and C + +

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.