The difference between C + + and Java __c++

Source: Internet
Author: User
Tags garbage collection inheritance multiple inheritance in c
One, C + + has the features that Java does not

Because Java itself is derived from C + +, here only to mention the features of C + + has been abandoned by Java-

00, Java no longer support pointers. This may be the biggest difference between C + + and Java.
"Note" Since there is no pointer in Java, there is no-> operator.
01, Java no longer support operator overloading.
02, Java no longer contains structure or union. These structures become redundant when the class that can fully contain them appears.
03, Java no longer includes preprocessing, and no longer supports preprocessing directives.
04, Java does not support automatic type conversion, you must display a forced type conversion.

05, the code in Java must be encapsulated in one or more classes.
"Note" Therefore no longer contains so-called global variables or global functions in Java.
06, Java no longer allow default parameters.
07, Java does not support multiple inheritance, that is, a subclass is not allowed to inherit multiple parent classes.
08. Although Java supports constructors, destructors are no longer supported. But Java adds the Finalize () function.
09, Java no longer support typedef.
10. No longer can declare unsigned integers in java.
11, Java no longer support goto statements.
12, Java no longer has the delete operator.
13. <> in Java no longer overloads I/O operations.
14. In Java, objects can be passed only by reference, and objects in C + + can be passed by value or reference.

Two, Java and C + + does not have the characteristics

Many of the features in Java are not in C + +. The three most important aspects are multithreading, packages, and interfaces, as well as many other unique features that enrich the Java programming environment.

00, multithreading. Java allows two or more identical threads to run concurrently. This is a concurrency mechanism supported at the language level. C + + does not have a similar mechanism, or C + + uses a single-threaded architecture. If you need to execute a C + + program concurrently, you must start it manually using the functionality of the operating system. Although both of these methods can execute two or more threads at the same time, the Java approach is clear and easy to use.

01, C + + has no corresponding to the Java package features. The most recent is a set of library functions with a common header file. However, building and using library functions in C + + is completely different from building packages and using packages in Java.

02. Java interfaces are similar to C + + abstract classes (abstract classes in C + + are classes that include at least one pure virtual function). For example, both abstract classes of C + + and Java interfaces cannot create instances. Both are used to specify a consistent interface for a subclass implementation. The biggest difference between the two is that the interface more clearly illustrates the concept.

03, Java provides a streamlined memory allocation mechanism (that is, the system automatically reclaims the memory mechanism). Like C + +, Java supports the new keyword. However, the DELETE keyword is not supported. When the last reference of an object is revoked, the object itself is automatically deleted and memory garbage collection is made. Consider that Java has no pointers, which makes the Java language security more powerful.

04. Java discarded the standard library of C + + and replaced it with its own collection of API classes. They are functionally similar, but the names and parameters are significantly different. Also, because all Java API libraries are object-oriented, and the C + + library is only partially, the library routines are called differently.

05. Java enhances break and continue statements to receive tokens.

06. The char type in Java is the international common 16-bit Unicode character set, so it automatically expresses the characters of most countries. This is similar to the wchar_t type in C + +. The use of Unicode characters enhances code portability.

07, Java added >>> operation, complete the unsigned right shift.

08. In addition to supporting single and multiline annotations, Java adds a third annotation method: documentation comments. Document comments to the end.
09, Java contains a built-in string type is called String. String is similar to a standard string class provided by C + + in some way. Of course, a string in C + + can only be used when declared in a program, and it is not a built-in type.

Three, Java and C + + are available but different aspects

00, Java, and C + + support Boolean-type data, but Java implements true and false in a different way than C + +. In C + +, true is not a 0 value, False is zero. In Java, true and false are predefined constants and are the only two values that a Boolean expression can get. Although C + + also defines true and false and is specified as a Boolean variable, C + + automatically converts a non-0 value to True and a 0 value to false. This situation does not occur in Java.

01. When creating a C + + class, the access descriptor is applied to a set of declarations. In Java, an access specifier is applied only to its qualified declaration.

02, C + + support exception handling, which is similar to Java, but in C + + does not need to catch a thrown exception.


Four, Java and C + + differences in detail (the expansion of some of the knowledge points)

00, Java running Speed (Java's biggest obstacle is speed)

The interpreted Java is about 20 times times slower than the C execution speed. Nothing can prevent the Java language from compiling. Some quasi real-time compilers have just appeared, and they can significantly speed up. Of course, we have every reason to think that there will be pure intrinsic compilers for more popular platforms, but without those compilers, there must be some problems that Java cannot solve due to speed constraints.

Many enterprise application developers are very fond of the language features of Java, but when developing important systems, the choice between language features and execution efficiency is often a headache. In critical computing, the user may not care how the data is compressed or how the delay relationship is set, but the speed at which the program is run is highly valued, which makes the developer of Java's compilation strategy a top priority. The Java language is now implemented in ways that do more than explain execution, and the Just-in-time compiler (JITC, just-in-time compiler) technology and prototyping technology have been adopted by many manufacturers, including Sun, IBM, Technology providers, including Oracle and Netscape, are leveraging these technologies to progressively improve Java execution, in which IBM has already integrated the Java Virtual machine (Jvm,javavirtual Machine), operating system, and hardware features organically, Effectively improves Java's execution efficiency.

01, Java All things must be placed in a class.

Java is a fully object-oriented language that no longer supports the process-design approach used by C + + programs, and all functions and variable parts must be part of the class. In addition to the basic data types (such as Integer, character, Boolean, and so on), other data are objects to Java, including arrays.

Objects combine data and methods to encapsulate them in a class so that each object can implement its own characteristics and behavior. There are no global functions or global data in Java. If you want to get the functionality equivalent to a global function, consider placing the static method and the static data in a class. C + + allows functions and variables to be defined as global. In addition, Java in the C and C + + to cancel the structure and union, enumerate this category of things, all Only "class" (class), eliminating unnecessary trouble.

Java syntax is actually referred to C + + syntax was born, but she removed multiple inheritance in C + +, pointers, delete and other difficult to grasp the technology, in order to facilitate migration and security, Java has adopted a pure object-oriented technology, even the main function public static void main ( String[] {} args) {} should also be placed in the main class to see the obvious differences from C + +.

02. In Java, class definitions take almost the same form as C + +.

But no sign ends the semicolon. There are no class declarations, only class definitions.

03, there is no scope in Java operator "::".

Java uses dots to do everything, but you don't have to think about it, because you can only define elements in one class. Even those method definitions must be within a class, so there is no need to specify scope for scopes. One of the differences we notice is a call to the static method: Using Classname.methodname ().

In addition, the name of the package (package) is established with the dot number, and can use the Import keyword to achieve C + + "#include" part of the function. #include并不直接映射成import, but it has a similar feeling when used. To use a class in another library, simply use the Import command and specify the library name. There is no macro similar to preprocessor.

C and C + + have a precompiled phase in the compilation process, known as preprocessor. The preprocessor provides convenience for developers, but increases the complexity of 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 (#include). (note: just like that)

04, Java has no preprocessing function of the detailed introduction

Java no longer has the function of #define, #include等预处理程序 (preprocessor). One of the most important features of the C + + language is its preprocessor. Some other programming languages, while adding #include functionality, still lack the ability to process macros (MACRO). #define的功能在Java中我们可以用定义常数 (constant), and #include is not needed in Java, because when a program is executed in Java, the type data is recorded in the object entity, and we do not need to rely on some header file ( Header file to know what data type the object or value we are using.

If you use the C + + language, only use the preprocessor #include and #define features, then you probably do not think Java such a design for you what kind of trouble, but if you are using the C + + language preprocessor in the macro function of the master, you may find it inconvenient, And then doubt the meaning of Java's design.

Although the use of preprocessor can easily achieve many functions, but from the point of view of software engineering, the maintenance of the entire software is actually very unfavorable. Because the functionality of the preprocessor in the C + + language is too powerful, powerful programming experts often develop a set of their own to understand the macro language, once the entire software to others to maintain, the successor is difficult in a short period of time to understand the previous person wrote macro functions, increase the software development team work and future maintenance difficulties.

Another point is the C + + language compiler to see the program code, in fact, and the program designers see the program code is different. The program designer sees the program code that has not been processed by the preprocessor, and the compiler sees the program code that the preprocessor has processed, and the error message generated by the compiler will not be expected by the programmer if the macro content is handled incorrectly by the preprocessor. This naturally adds to the difficulty of scheduling the program.

The existence of preprocessor also causes the inconvenience of reading programs. If you want to use a C + + language program that someone else has already completed, you will not only have to read the file he wrote, but you must also consult the file to see the full picture of the program. If you are replacing Java programs, just look at the Java program files.

05, similar to C + +, Java contains a series of "main types" (primitive type) to achieve more efficient access in Java, these types include Boolean,char,byte,short,int,long, float and double. The size of all primary types is intrinsic and independent of the specific machine (considering the problem of porting). This will certainly have a certain impact on performance, depending on the different machines. Checking and asking for types is becoming more demanding in Java.

For example:

A conditional expression can only be a Boolean (Boolean) type, and an integer cannot be used.

You must use the result of an expression like x+y; you can't just use "x+y" to implement "side effects."

06. Java statically referenced strings are automatically converted to string objects. Unlike Java and C + +, there is no independent static character array string to use. C and C + + do not support string variables, and use null terminators in C and C + + programs to represent the end of strings, which are implemented in Java by Class objects (string and StringBuffer), which are the core of the Java language. Implementing a string with a class object has several advantages:

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

b The Java string class is defined as part of the Java language, not as an additive extension;

c) Java strings perform run-time checks to help troubleshoot some run-time errors;

d) The string can be connected with a "+" number.

07, Java added three right shift operator ">>>", with the "logical" right shift operator similar to the function, can be inserted at the end of the 0 value. ">>" Inserts a sign bit (or "arithmetic" shift) at the same time as the shift.

08, although superficially similar, but compared with C + +, Java array is a rather different structure, and has unique behavior. That is, Java provides the data subscript out of bounds check, and C + + is not provided.

There is a read-only length member, through which you know how large the array is. And once the array bounds are exceeded, the runtime check automatically discards an exception. All arrays are created in the memory heap, and we can assign one array to the other (simply copy the array handle). An array identifier is a first-level object, and all of its methods usually apply to all other objects.

09, for all objects that are not of the main type, can only be created through the new command and C + +, Java does not have the appropriate command to "on the stack" to create objects that are not of the main type. All primary types can only be created on the stack without using the new command. All major classes have their own "wrapper" classes, so you can create equivalent, memory-heap based objects with new (an exception to the main type array: They can be assigned as C + + by initializing the collection, or using new).

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.