The difference between Java and C, C + +

Source: Internet
Author: User

First, it should be clear that Java is developed by C + +, and retains much of C + +, which is programmed in a similar way to C + +. But Java's syntax is clearer, smaller and easier to learn. Sun's extensive research into a variety of programming languages, and the abandonment of other language deficiencies, eventually dropped out of Java. In this way, Java fundamentally solves the inherent flaws of C + + and forms a new completely object-oriented language.

There are more similarities between Java and C + +, and C-based readers, it is easier to learn java. In comparison, Java's programming environment is much simpler. Due to space limitations, there is no place to make a complete list of differences, only a few of the more notable differences are listed.

Pointers
Java does not have the concept of pointers, which effectively prevents the error of pointer manipulation (such as a system crash caused by a pointer dangling) in C + + languages. When the pointer is manipulating memory in C + +, errors often occur. There are no pointers in Java, which is more useful for Java program security.

Multiple Inheritance
C + + supports multiple inheritance, which allows a multi-parent class to derive a subclass. That is, a class allows inheriting multiple parent classes. 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. Therefore, Java does not support multiple inheritance, but allows a class to implement multiple interfaces. It can be seen that Java not only realizes the function of multiple inheritance of C + +, but also avoids many defects of C + +.

Data Type
Java is a fully object-oriented language, and all methods and data must be part of a class. In addition to the basic data types, data of the remaining types is used as Object-type data. For example, object type data includes strings and arrays. Classes combine data and methods to encapsulate them, so that each object can implement its own behavior. C + + defines functions and variables as global, and then calls these functions and variables, which increases the burden on the program. In addition, Java also cancels the structure and union in C + +, making the compiler more concise.

Automatic memory Management
All objects in a Java program are built on the stack with the new operator, which resembles the "new" operator of C + +. Java automates garbage collection operations without the need for a programmer to delete them. When an object in Java is no longer being used, you do not need to use the memory collector, just label it as a delete. Garbage collector runs in the background and works with idle time. In C + +, memory resources must be freed by programs, which increases the burden on the programmer.

operator Overloading
Java does not support operator overloading, and operator overloading is considered a prominent feature of C + +. In Java, although classes can implement such functionality, they do not support operator overloading, which is to keep the Java language as simple as possible.

preprocessing Capabilities
C + + has a pre-compilation phase in the compilation process, which is pre-processor. The preprocessor provides convenience for developers, but adds complexity to the compilation. Java allows preprocessing, but does not support preprocessor functionality, because Java does not have a preprocessor, so in order to implement preprocessing, it provides the introduction statement (import), which is similar to the functionality of a C + + preprocessor.

Java does not support default function parameters, while C + + supports.
In C, the code is organized in functions, and functions can access the program's global variables. C + + adds classes and provides class algorithms, which are functions that are connected to classes, and C + + class methods are very similar to Java class methods. c c is still supported in C + + programs, so you can still make use of the functions that are used in the program, resulting in a mix of functions and methods, which makes C + + programs confusing.

Java has no functions as an object-oriented language that is more pure than C + +. Java forces developers to include all the routines in the class. In fact, using methods to implement routines can motivate developers to better organize coding.

string
C and C + + do not support string variables, using the "Null" Terminator in C and C + + programs to represent the end of the string, in Java strings are implemented using class objects (string and StringBuffer), and the method of building strings and accessing string elements is consistent throughout the system. Java string classes are defined as part of the Java language, not as an additional extension. In addition, Java can also use "+" for strings to connect operations.

Goto Statement
The "scary" goto statement is the "relic" of C and C + +. It is the legal part of the language technology, the reference goto statement causes the program structure confusion, difficult to understand. Goto statements are generally used to unconditionally transfer subroutines and multi-structure branching techniques. Java does not provide a goto statement, although it specifies goto as the keyword, but does not support its use, which makes the program more concise and readable.

Type Conversions
In C and C + +, implicit conversions of data types sometimes occur, which involves the 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.

The difference between Java and C, 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.