Java and C + + differences in the detailed

Source: Internet
Author: User

Java and C + + are both object-oriented languages. In other words, they are able to implement object-oriented thinking (encapsulation, relay, polymorphism). And because C + + in order to take care of a large number of C language users, but compatible with C, so that itself only become a class with C language, more or less affect the thoroughness of its object-oriented! Java is a complete object-oriented language, its syntax is clearer, smaller, and easier to learn. It is based on a detailed study of a variety of programming languages, according to abandon the shortcomings of other languages, fundamentally solve the inherent defects of C + +.

Java and C + + have more similarities than differences, but the two major differences in the language make Java easier to learn, and the programming environment simpler.

I am not here to list the differences entirely, but to list the more significant differences:

1. Pointers

The Java language makes it impossible for programmers to find pointers to directly access memory without pointers, and adds automatic memory management capabilities to effectively prevent pointer manipulation errors in C + + languages, such as system crashes caused by wild pointers. But it is not that Java has no pointers, the virtual machine is still using the pointer, but outsiders can not use it. This facilitates the security of Java programs.

2. Multiple inheritance

C + + supports multiple inheritance, which is a feature of C + + that allows multiple parent classes to derive a class. Although multiple inheritance is powerful, complex, and can cause a lot of trouble, it is not easy to compile programs to implement it. Java does not support multiple inheritance, but allows a class to inherit multiple interfaces (extends+implement), achieve multiple inheritance functions of C + +, and avoid the inconvenience caused by multiple inheritance implementations in C + +.

3. Data type and Class

Java is a fully object-oriented language, and all functions and variable parts 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, Java eliminates the unnecessary hassle of eliminating the structure and the Union in C + +.

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 C + + operator. 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 an instance of read is established on the stack structure. Java automates garbage collection operations without the need for programmers to delete them. and the C 20 must release the memory resources by the program shell, increased the program designer's negative throw. In Java, when an object is not reused, the garbage collector will label it as a deletion. In Java, the garbage collector runs in the background in a threaded fashion, working with idle time.

5. Operator overloading

Operator overloading is not supported in Java. Operator overloading is considered to be 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. pretreatment function

The preprocessing feature is not supported in Java. C/E 20 has a pre-warp phase in the compilation process, known as a preprocessor. The preprocessor provides convenience for developers, but increases the complexity of butyl 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, code is organized in functions, and functions can access the program's global variables. C 20 Adds a class, provides a class algorithm, which 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 it is not possible to prevent C 20 developers from using functions, resulting in mixed use of functions and methods to make the program more confusing.

Java has no functions, as a more pure object-oriented language than C 20, Java forces developers to include all routines in the class, and in fact, using methods to implement routines can motivate developers to better organize their code.

8 string

C and C 20 do not support string variables, the use of NULL terminators in C and C 20 programs to represent the end of strings, in Java strings are implemented using class objects (STRINR 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 the string and accessing the string elements in the whole system is consistent;

(2) J3-Yang string classes are defined as part of the Java language, not as an additive extension;

(3) The Java string performs runtime check NULL to help troubleshoot some run-time errors;

(4) The string can be connected with "ten".

9 "goto Statement"

The "terrible" goto statement is the C and C + + "relics", which is the legal part of the language, the reference to the GOTO statement caused the confusion of the structure of the program, difficult to understand, the goto sentence to be used for unconditional transfer subroutine and multiple structure branch technology. Given a wide range of reasons, Java does not provide a goto statement, although it specifies goto as a keyword, but does not support its use, making the program simple and readable.

L0. Type conversion

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 necessary, must be explicitly coerced by the program.

11. Abnormal

Exception mechanisms in Java are used to catch exception events and enhance system fault tolerance

try{//code that may produce exceptions

}catch (exceptiontype name) {

Processing

}

Where Exceptiontype represents 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.