What is the difference between Java and C/C ++?
(1) memory allocation in Java isDynamicIt uses an object-oriented mechanism and uses the new operator to allocate memory space for each object. In addition, the actual memory will change as the program runs. when the program is running, the Java System Automatically scans the memory and collects unused space for a long time as "garbage" to make full use of system resources. according to this mechanism, programmers do not need to pay attention to memory management issues, which makes Java programming simple and clear, and avoids system problems caused by memory management errors. the C language uses the two library functions malloc () and free () to allocate internal and memory space, respectively, in C ++, memory is allocated and released through the new and delete operators. in the C and C ++ mechanisms, programmers must carefully handle memory usage issues. on the one hand, if you release the released memory or release the memory that has not been allocated, it will cause a crash. On the other hand, if you do not release memory that is not used for a long time or no longer, it will waste system resources, and even cause resource depletion.
(2) Java is not defined outside all classesGlobal VariablesDefines a public static variable in a class to complete the global variable function.
(3) JavaGOTO statement not requiredInstead of using the try-catch-finally Exception Handling statement to handle errors.
(4) JavaHeader files not supportedIn C and C ++, header files are used to define the class prototype, global variables, library functions, etc. This header file structure makes the system operation and maintenance quite complicated.
(5) JavaMacro definition not supportedInstead, the keyword final is used to define constants. In C ++, macro definitions are used to define constants, which is not readable for programs.
(6) Java assigns a fixed length for each data type. for example, in Java, the int type is always 32 bits, while in C and C ++, different bytes are allocated for different platforms for the same data type, it is also the int type, in the PC is two bytes is 16 bits, and in the VAX-11, is 32 bits. this makes C language portable, while Java is cross-platform (platform-independent ).
(7)Type conversion is different. In C and C ++, any type conversion through pointers is often insecure. in Java, the runtime system checks the type compatibility for object processing, to prevent insecure conversions.
(8)Structure and joint processing. In C and C ++, all the members of the structure and union are public, which leads to security issues. Java does not contain the structure and union at all, all contents are encapsulated in the class.
(9) JavaNo longer use pointer. Pointers are the most flexible and error-prone data types in C and C ++. memory Address operations performed by pointers often result in unpredictable errors. When a memory address is explicitly converted using pointers, you can access a private member in C ++, this damages the security. java fully controls pointers, so programmers cannot directly perform any pointer operations.
(10) Java runs in JVM and is compiled into JVM to recognize and load. class is not a program that can be directly compiled into a computer. c ++ can be directly executed in a computer.
Program Running EfficiencyC ++ must be more efficient than Java programs