Java and C + + comparison

Source: Internet
Author: User

Overall difference

1.

C + + is run directly on the machine (after compiling the machine code), and Java compiles the *.class file (bytecode) is run on the Java Virtual Machine (JVM), after the JVM Interpreter (machine code) and then put on the real machine to run. The JVM is the equivalent of using software to construct a computer with its own CPU (fake) and memory (also fake). *.exe machine Java (*.class)->jvm-> machine   
2.
Java is born for the web. Hides the underlying implementation details, as well as confusing pointers to the concepts that lead to unsafe results such as memory leaks, allowing developers to focus more on business logic, the so-called purely object-oriented.
C + +, after all, is a compiled language, the bottom of the ability to manipulate, retaining pointers to these directly operating memory elements, than the Java interpretation of the advantages of the language is high efficiency. 
more significant differences
1. Pointers
The Java language makes it impossible for programmers to find pointers to direct access to memory without pointers, and to add automatic memory management capabilities, effectively preventing pointer manipulation errors, such as system crashes caused by wild pointers, in the C + + language. 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:
read R=new read ();
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 20, it is necessary for the programmer to release the memory resources, which increases the burden of the program designer. 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
operator overloading is not supported in Java. Operator overloading is considered a prominent feature of C 20, 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 capabilities
The preprocessing feature is not supported in Java. A pre-coding phase, known as a pre-processor, is available during the compilation process. 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 the C 20 preprocessor.
7. Java does not support default function parameters, while C 20 supports
In C, the code is organized in functions, and functions can access the program's global variables. C 20 adds the class, provides the class algorithm, the algorithm is a function connected to the class, the C 20 class method is very similar to the Java class method, however, because C 20 still supports C, so C 20 developers can not prevent the use of functions, the result of mixed use of functions and methods to make the program more chaotic.
Java has no functions, and as a more pure object-oriented language than C 20, Java forces developers to include all the routines in the class, in fact, implementing routines in a way that motivates developers to better organize coding.
8 String
C and C 20 do not support string variables, using a null terminator in C and C 20 programs to represent the end of a string, in Java strings are implemented with class objects (string 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 runtime check empty, can help to exclude some runtime errors;
(4) The string can be connected by "ten".
9 Goto Statement
The "scary" goto statement is the "relic" of C and C + +, which is the legal part of the language, and the reference goto statement causes the confusion of the program structure, which is difficult to understand, and the goto sentence is used for the unconditional transfer subroutine and the multi-structure branching technique. 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
implicit conversions of data types sometimes occur in C and C 20, which involves automatic coercion of type conversions. For example, in C 20, 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 20 and, if required, must be explicitly enforced by the program for type conversions.
11. Exceptions
exception mechanisms in Java are used to capture exception events and enhance system fault tolerance
try{//code that may produce exceptions
}catch (exceptiontype name) {
//Processing
}
where Exceptiontype represents the exception type. and C + + does not have such a convenient mechanism.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java and C + + comparison

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.