Differences between C/C ++ and Java

Source: Internet
Author: User

Differences between C/C ++ and Java
Difference between Java and C/C ++

See: http://docs.rinet.ru/Jintra/appe.htm
Translation of I. a_7

 

Preprocessor ):
All C/C ++ compilers have a stage of processing in the compilation process called a pre-processor. the C ++ pre-processor mainly searches for and replaces the identifiers declared by the # define, # typedef and other indicators. although most C ++ advocates do not advocate the use of features inherited from C, this usage is still widely used in C ++ programmers. in C ++, most pre-processors are stored in the header files used to supplement the actual code files.

The problem caused by the use of pre-processors is that programmers can easily increase the complexity of the program inadvertently. one example is that in a project, many programmers use the # define, # typedef and other indicators to mark a series of nouns they have created, the direct result is that other programmers have to view all header files to find the corresponding information to understand the program, which makes code maintenance and reuse almost impossible. another problem with the pre-processor method is the lack of sufficient stability during type or validity checks.

Java does not have a Preprocessor. it provides functions similar to the C ++ pre-processor and improves control. use constant data members to replace # define indicators, and use class definitions to replace # typedef usage. this makes Java source code more coherent and readable than C ++. in addition, Java programs do not use header files. Java compilers directly create class definitions in the source code that includes both class definitions and method implementations.

 

Pointer
Most developers agree that pointer misuse is the main cause of program errors when using C/C ++ programming. in short, when you have pointers, you can destroy the memory. c ++ programmers generally use complex pointer operations to create and maintain dynamic data structures, the result is that C ++ Programmers spend a lot of time solving the strange problem caused by the complicated pointer operation.

Pointers are not supported in Java. java implements operations similar to pointers by providing a large number of references. in Java, all arrays and objects are passed through references. this method avoids Common Errors Caused by pointer error management. this simplifies programming in many ways, because in addition to experienced programmers, the average person can easily confuse the correct pointer usage.

You may think that, because there is no pointer, many data structures such as dynamic arrays cannot be implemented in Java. in fact, any task that can be completed by pointers can be implemented more reliably using objects and array objects. you can also benefit from the security guarantee provided by Java's runtime system, such as the boundary check for all array indexes.

 

Structures and unions)
There are three complex data types in C ++: Class, structure, and union. java only implements one of the three types: class. java forces the user to use classes to replace the structure or union to implement the required functions. although it sounds like programmers need to do more work for this, in fact, because classes can simply simulate the structure and union, this makes the code more coordinated. java designers sincerely hope to maintain the simplicity of the language, so they eliminate the duplicates in the language features.

Functions ):
In C, the code is organized by the functions built by subprograms. c ++ adds the concepts and methods of classes, that is, functions related to classes. the class methods in C ++ are very similar to those in Java. however, C ++'s support for C makes it impossible for C ++ programmers to use functions in classes. the mixed use of functions and methods directly causes confusion in program understanding.

Java does not have the function concept. as a more pure object-oriented language than C ++, Java forces programmers to integrate all programs into methods. there are no other restrictions on using methods instead of functions. the result is that implementing programs in the form of Methods encourages programmers to better organize code structures. remember this: using a function does not cause any program errors. it only does not match the object-oriented essence in the Java example.

 

Multiple inheritance)
Multi-inheritance is a feature of C ++, allowing you to derive child classes from different parent classes. although multiple inheritance is sometimes a powerful Inheritance Method, many problems often occur due to its complexity. from the perspective of compilers, multi-inheritance is also complicated and difficult to implement.

Java chooses another method without directly supporting multiple inheritance. you can use the Java interface (interfaces) to implement functions similar to multi-inheritance. java interfaces provide object method descriptions without implementation.

String (strings)
C and C ++ do not add text strings to their built-in types. A standard technique used in C and C ++ is to mark strings in an array with null as the Terminator.

In Java, strings are implemented as the first class objects (string and stringbuffer), which means Java provides kernel-level support for strings. java has the following advantages when using objects to implement strings:
. Create strings across platforms and languages and manage element access.
. Because strings in Java are defined as part of the language rather than extensions, this makes Java string functions predictable (predicatably ).
Java executes extensive runtime checking, which can help eliminate some tricky runtime errors ).

 

GOTO statement (the GOTO statement)
Even in C and C ++, daunting goto statements are controversial, but technically, Goto statements are part of the legal language. the long-standing criticism of the GOTO statement is that it may cause disorder and unreadable code structure, and sometimes it may even lead to an unpredictable so-called "Pasta. the GOTO statement is applicable to the more structured branch technology where the jump is made without judgment.

For one reason or another, Java does not support the GOTO statement. the Java language specifies goto as a keyword, but does not support it. I think Java designers even want to eliminate the possibility of using "Goto" as an identifier. the Java language that does not contain the GOTO statement simplifies the language and avoids code with messy structure.

 

Operator Overloading)
Operator Overloading is a significant feature of C ++, which is not supported by Java. although Java provides similar functions through some implementations of classes, it still lacks some convenience brought by operator overloading. however, from the perspective of Java protection, Operator Overloading may sometimes become too tricky ). undoubtedly, Java developers make Java as simple as possible by making the language not support the decision of operator overloading.

 

Automatic reduction)
Automatic reduction is related to implicit conversion of data types in C/C ++. for example, in C ++, you can assign a float value to an int variable, which will lead to loss of some information. java does not support automatic reduction of the C ++ style. in Java, if a certain reduction causes data loss, you must explicitly specify the data type to be converted.

Variable Arguments)
C/C ++ allows you to declare a function to use variable parameters, such as printf. although this is a convenient feature, it also makes it impossible for the compiler to check the complete parameter types, which means that some problems you don't know will occur during the runtime. variable parameters are not supported in Java.

 

Command-line arguments)
The command line parameters passed by the system to the Java program are completely different from those passed to the C ++ program. restricted. The number of parameters passed to the two languages is different. in C/C ++, the system passes two parameters to the program: argc and argv. The former specifies the number of parameters stored in argv, the latter is a character pointer pointing to the address storing the real parameter group. in Java, the system passes a separate value to the program: args. ARGs is a string array containing command line parameters.

In C/C ++, the command line parameter passed to the program contains the name of the called function. this rarely used name is saved as the first real parameter. in Java, you already know the name of the program with the same class name, so you do not need to pass the program name as a command line parameter. therefore, the Java runtime system only transmits the parameters after the name of the called program.

 

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.