Java Program Ape interview written Book of the Java and C + + What are the similarities and differences

Source: Internet
Author: User
Tags goto

Both Java and C + + are object-oriented languages that use object-oriented thinking (such as encapsulation, inheritance, polymorphism, and so on), because object-oriented has many very good features (inheritance, composition, etc.) that make both of them very well reusable.

It is important to note that the two are not exactly the same, and the following are the main differences:

(1) Java is interpreted as a language, the process of running is: The source code after the Java compiler compiled into bytecode, and then run by the JVM interpretation. The C + + is a compiled language, and the source code is compiled and linked to generate the binary codes that can be run. As a result, Java runs slower than C + +, but Java is able to run across platforms, while C + + cannot.

(2) Java is a pure object-oriented language, all the code (including functions, variables, etc.) must be implemented in the class, except for the basic data types (including int, float, etc.), all types are classes. In addition, there are no global variables or global functions in the Java language, and C + + has both process-oriented and process-oriented programming features to define global variables and global functions.

(3) compared with the C + + language, the Java language does not have the concept of pointers, which effectively prevents the system problems that can be caused by manipulating pointers in C + + languages, making the program more secure.

(4) compared with the C + + language, the Java language does not support multiple inheritance, but the Java language introduces the concept of interface, can implement multiple interfaces at the same time, because the interface also has polymorphic characteristics, so in the Java language can be implemented by implementing multiple interfaces to the C + + language of multiple inheritance similar purposes.

(5) in the C + + language, developers are required to manage the allocation of memory (including application and release), while the Java language provides a garbage collector to implement garbage collection itself, without requiring the program to explicitly manage the allocation of memory. In the C + + language, the code that frees resources is usually placed in a destructor, and the Java language does not have a destructor concept, but a finalize () method is introduced that, when the garbage collector is about to release the memory of a useless object, calls the object's Finalize () method first, so Developers do not need to be concerned about or need to know when the memory space occupied by an object is released.

(6) The C + + language supports operator overloading, while the Java language does not support operator overloading. The C + + language supports preprocessing, while the Java language does not have a preprocessor, although preprocessing features (including header files, macro definitions, and so on) are not supported, but it provides an import mechanism similar to the preprocessor features in C + +. C + + supports default function parameters, while Java does not support default function parameters. C + + supports GOTO statements, while Java does not provide goto statements (but in Java goto is reserved keyword). C + + supports its own proactive coercion of type conversions, which can lead to unsafe programs, and Java does not support its own initiative to enforce type conversions, which must be explicitly enforced by the developer. All members of a struct and a union are public, which often leads to security problems, and Java does not contain structures or unions at all, and all of the content is encapsulated within the class.

(7) Java has platform independence, that is, each data type is assigned a fixed length, for example, the type of int always occupies 32 bits, and C + + is not, the same data type on different platforms will be allocated a different number of bytes.

(8) Java provides built-in support for gaze documents, so source files can also contain their own documents. With a separate program, these document information can be extracted and formatted into HTML again.

(9) Java contains a number of standard libraries, used to complete specific tasks, the same time these libraries are easy to use, can greatly shorten the development cycle, such as Java to provide access to the database of the JDBC library for the implementation of distributed object RMI and other standard libraries. C + + relies on some non-standard libraries provided by other vendors.

Common written Questions:

The following statements are wrong ()

A:java Object-oriented language allows individual processes and functions to exist

B:java Object-oriented language allows separate methods to exist

Methods in the C:java language belong to members in the Class (member)

A method in a D:java language must be subordinate to a class (object), and the calling method is the same as a procedure or function

Answer: A, B, C. See above for explanations.


From the Java Program Ape interview written book

Java Program Ape interview written Book of the Java and C + + What are the similarities and differences

Related Article

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.