Java and C + + are object-oriented languages. In other words, they are capable of implementing object-oriented thinking (encapsulation, multiplication, polymorphism). And because C + + in order to take care of a large number of C-language users, and compatible with C, making itself only a band-Class C language, more or less affect the thoroughness of its object-oriented! Java is a completely object-oriented language, with a clearer syntax, smaller size, and easier learning. It is based on a thorough and detailed study of a variety of programming languages, and it abandons the shortcomings of other languages and fundamentally solves the inherent defects of C + +.
There are more similarities between Java and C + +, but there are several major differences between the two languages that make Java easier to learn, a brother with Java EE training, and a simpler programming environment.
I cannot completely list the differences here, only the more significant differences are listed:
1. Pointers
The Java language makes it impossible for programmers to find pointers to direct access to memory without pointers, and to add automatic memory management capabilities, effectively preventing pointer manipulation errors, such as system crashes caused by wild pointers, in the C + + language. But it is not that Java does not have pointers, the virtual machine inside or use a pointer, but outsiders are not allowed to use it. This facilitates the security of Java programs.
2. Multiple inheritance
C + + supports multiple inheritance, a feature of C + + that allows a class to be derived from a multi-parent class. Although multiple inheritance is powerful, it is complex to use and can cause a lot of trouble, and it is not easy for the compiler to implement it. Java does not support multiple inheritance, but allows a class to inherit multiple interfaces (Extends+implement), realizes the function of multiple inheritance of C + +, and avoids the inconvenience caused by multiple inheritance implementations in C + +.
3. Data types and Classes
Java is a fully object-oriented language, and all functions and variables 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, the structure and union in C + + is removed in Java, eliminating unnecessary hassles.
4. Automatic memory management
All objects in a Java program are built on the memory stack with the new operator, which resembles the new operator of C + +. 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 a read instance was created on the stack structure. Java automates garbage collection operations without the need for a programmer to delete them. and C 20 must be released by the program Shell memory resources, increase the program designer's negative throw. In Java, when an object is not reused, the garbage collector adds a label to it to delete it. Useless memory recycling programs in Java run in the background in a threaded fashion, using idle time to work.
5. Operator overloading
Operator overloading is not supported in Java. Operator overloading is considered 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. Preprocessing function
The preprocessing feature is not supported in Java. A pre-coding phase, known as a pre-processor, is available during the compilation process. The preprocessor provides convenience for developers, but increases the complexity of the Ding 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, the code is organized in functions, and functions can access the program's global variables. C 20 adds the class, provides the class algorithm, the algorithm 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 C 20 developers can not prevent the use of functions, the result of mixed use of functions and methods to make the program more chaotic.
Java has no functions, and as a more pure object-oriented language than C 20, Java forces developers to include all the routines in the class, in fact, implementing routines in a way that motivates developers to better organize coding.
8 string
C and C 20 do not support string variables, using a null terminator in C and C 20 programs to represent the end of a string, in Java strings are implemented with 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 strings and accessing string elements is consistent throughout the system;
(2) J3 the string class is defined as part of the Java language, not as an additional extension;
(3) Java string execution run-time check-out to help troubleshoot some runtime errors;
(4) The string can be connected by "ten".
9 "goto statement
The "scary" goto statement is the "relic" of C and C + +, which is the legal part of the language, and the reference goto statement causes the confusion of the program structure, which is difficult to understand, and the goto sentence is used for the unconditional transfer subroutine and the multi-structure branching technique. Given the wide justification, Java does not provide a goto statement, which, while specifying Goto as the keyword, does not support its use, making the program simple and readable.
L0. Type conversions
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 required, must be explicitly enforced by the program for type conversions.
11. Exceptions
Exception mechanisms in Java are used to capture exception events and enhance system fault tolerance
try{//code that may produce exceptions
}catch (exceptiontype name) {
Processing
}
Java and C + + differences