Differences between Java and C ++

Source: Internet
Author: User

Java and C ++ are both object-oriented languages. That is to say, they can all implement object-oriented ideas (encapsulation, multiplication, polymorphism ). In order to take care of a large number of C language users,

C compatibility makes C only a C language with classes, which affects the thoroughness of object orientation more or less! Java is a fully object-oriented language with clearer syntaxes, smaller scales, and easier to learn. On the basis of in-depth and meticulous research on multiple programming languages, it discards the shortcomings of other languages and fundamentally solves the inherent defects of C ++.

Java and C ++ have more similarities than differences, but the main differences between the two languages make Java easier to learn and make the programming environment easier.

I cannot list the differences here, but only the obvious differences are listed:

1. pointer

Java makes it impossible for programmers to find pointers to directly access the memory without pointers, and adds an automatic memory management function to effectively prevent pointer operation errors in the C/C ++ language, such as system crash caused by the wild pointer. But it doesn't mean that Java has no pointer. Pointers are still used inside the virtual machine, but they are not used by outsiders. This is conducive to Java program security.

2. Multi-Inheritance

C ++ supports multiple inheritance, which is a feature of C ++. It allows multiple parent classes to derive a class. Although the multi-inheritance feature is strong, it is complicated to use and can cause a lot of trouble. It is not easy to compile a program to implement it. Java does not support multi-inheritance, but allows a class to inherit multiple interfaces (extends + implement), implementing the C ++ multi-inheritance function, it also avoids the inconvenience caused by multiple inheritance implementation methods in C ++.

3. data types and classes

Java is a fully object-oriented language. All functions and variables must be part of the class. Except for the basic data type, all other objects are class objects, including arrays. Objects combine data and methods and encapsulate them in classes so that each object can implement its own characteristics and behavior. C ++ allows you to define functions and variables as global. In addition, Java removes the structure and union in C/C ++, eliminating unnecessary troubles.

4. Automatic Memory Management

All objects in the Java program are built on the memory stack using the new operator, which is similar to the new operator of C ++. The following statement creates a read-like object and then calls the work method of the object:

Read r = new read ();

R. Work ();

Statement read r = new read (); A read instance is created in the stack structure. Java automatically recycles useless memory and does not need to be deleted by programmers. In C, the program shells must release memory resources and increase the negative throwing of program designers. In Java, when an object is not reused, the useless memory recycler adds tags to it to indicate deletion. Java useless memory recycle programs run in the background in the thread mode and work in idle time.

5. Operator Overloading

Java does not support Operator overloading. Operator Overloading is considered to be a top ten feature of C. Although classes in Java can basically implement such a feature, the convenience of Operator Overloading is still lost. Java does not support Operator Overloading to keep Java as simple as possible.

6. Preprocessing

Java does not support preprocessing. C/C has a well-known pre-processor in the compilation process. The pre-processor provides convenience for developers, but increases the complexity of Ding compilation. The Java virtual machine does not have a Preprocessor, but it provides import statements similar to the functions of the C 10 pre-processor.

7. Java does not support default function parameters, but C 10 supports them.

In C, the code is organized in the function, and the function can access the global variables of the program. Class is added to Class C 10 and class algorithms are provided. This algorithm is a function connected to the class. Class C 10 methods are very similar to Java class methods. However, since Class C 10 still supports Class C, therefore, it is not possible to prevent C 10 developers from using functions. The mixed use of result functions and methods makes the program messy.

Java does not have a function. As a language that is more pure than the top ten object-oriented languages of C, Java forces developers to include all routines in the class. In fact, routine implementation with methods can motivate developers to better organize codes.

8 string

C and C 10 do not support string variables. The null Terminator is used in C and C 10 programs to indicate the end of the string. in Java, the string is a Class Object (strinr and stringbuffer) these class objects are the core of the Java language. Using class objects to implement strings has the following advantages:

(1) the method for establishing strings and accessing string elements in the entire system is consistent;

(2) The J3 Yang string class is defined as part of the Java language, rather than as an extension of the plus;

(3) the Java string runtime check is empty, which helps to eliminate some running errors;

(4) You can use "10" to connect strings.

9 "GOTO statement

The "terrible" GOTO statement is a "relic" of C and C ++. It is a legal part of the language technology. referencing a GOTO statement causes confusion in the program structure and is hard to understand, the Goto substatement must be used for unconditional transfer of subprograms and multi-structure branch technology. For a wide range of reasons, Java does not provide a GOTO statement. Although it specifies a goto as a keyword, it does not support its use, making the program simple and easy to read.

L0. type conversion

Implicit conversions of Data Types sometimes occur in the 10th of C and C, which involves automatic forced conversions. For example, you can assign a floating point value to an integer variable in December 10, c, and remove its ending number. Java does not support automatic forced type conversion in the top 10 C program. If necessary, the program must explicitly perform forced type conversion.

11. Exceptions

The exception mechanism in Java is used to capture exception events and enhance the system fault tolerance capability.

Try {// code that may generate exceptions

} Catch (exceptiontype name ){

// Process

}

Predictiontype indicates the exception type. C ++ does not have such a convenient mechanism.

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.