Java interview written test-002-What are the similarities and differences between Java and C/C++

WeChat public account: JavaWeb architect

describe

1) Java is an interpreted language.

The running process is as follows: the program source code is compiled into bytecode by the Java compiler, and then interpreted and executed by the JVM. C/C++ is a compiled language, and the source code is compiled and linked to generate executable binary code. So Java executes slower than C/C++, but Java is able to execute cross-platform while C/C++ cannot.

2) Java is a pure object-oriented language.

All code (including functions, variables, etc.) must be implemented in classes, and all types except basic data types (including int, float, etc.) are classes. In addition, there is no global variable or global function in the Java language, while C++ has both process-oriented and process-oriented programming characteristics, and global variables and global functions can be defined.

3) There is no concept of pointers in Java language.

This effectively prevents system problems that may be caused by manipulating pointers in the C/C++ language, thereby making the program more secure.

4) Java language does not support multiple inheritance.

But Java language introduces the concept of interface, which can implement multiple interfaces at the same time. Because the interface also has polymorphism characteristics, in the Java language, you can achieve a similar purpose with multiple inheritance in the C++ language by implementing multiple interfaces.

5) The Java language provides a garbage collector to realize automatic garbage collection.

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 achieve automatic garbage collection, no: the program needs to explicitly manage the allocation of memory. In the C++ language, the code for releasing resources is usually placed in the destructor. Although there is no destructor in the Java language, a finalize() method is introduced. When the garbage collector is about to release the memory of useless objects, The finalize() method of the object is called first, so the developer does not need to care or know when the memory space occupied by the object will be freed.

6) Others.

1.C++ language supports operator overloading, while Java language does not support operator overloading.

2. The C++ language supports preprocessing, while the Java language does not have a preprocessor. Although it does not support preprocessing functions (including header files, macro definitions, etc.), the import mechanism it provides is similar to the preprocessor function in C++.

3. C++ supports default function parameters, while Java does not support default function parameters. C/C++ supports goto statement, but Java does not provide goto statement (but goto is a reserved keyword in Java).

4.C/C++ supports automatic type conversion, which will lead to unsafe programs; while Java does not support automatic type conversion, it must be explicitly cast by the developer.

5. In C/C++, all members of structures and unions are public, which often leads to security problems, while Java does not contain structures and unions at all, and everything is encapsulated in classes.

6. Java is platform independent, that is, a fixed length is allocated to each data type. For example, the int type always occupies 32 bits, but C/C++ is not. The same data type will be allocated different words on different platforms. Section number.

7.Java provides built-in support for annotated documentation, so source files can also contain their own documentation. Through a separate program, this document information can be extracted and reformatted into HTML.

8. Java includes some standard libraries for completing specific tasks, and these libraries are easy to use and can greatly shorten the development cycle. For example, Java provides JDBC libraries for accessing databases and RMI for implementing distributed objects. etc. standard library. C++ relies on some non-standard libraries provided by other vendors.


Common Interview Questions

Among the following statements, there are ( ) errors.
A. The Java object-oriented language allows separate procedures and functions to exist.
B. The Java object-oriented language allows separate methods to exist.
C. The methods in the Java language are members of the class (member)
D). The methods in the Java language must belong to a certain class (object), and the calling method is the same as the procedure or function.

Answers: A, B, C. See explanation above.


other

Source code download:

关注下方微信公众号,
回复:
JavaMB.code

Full tutorial PDF version download

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324729102&siteId=291194637