Basic differences between Java and C ++

Source: Internet
Author: User
Tags define function type casting
Basic differences between Java and C ++-Linux general technology-Linux programming and kernel information. The following is a detailed description. Preface

Java groups are expanding every day, including some of the world's largest ISVs, it also includes the company's CIOs, information technology personnel, system analysts, C/S developers, programmers, multimedia designers, marketing staff, educators, managers, Video Producers and even amateurs.. Traditionally, it is rare for such people to work effectively together. When talking about open systems, we often talk about published APIs and specifications, source code availability, or hardware, networking, and operating systems, no one is talking about people in the open sense. Java completes the closed chain of open systems. It develops human resources and, in turn, opens up the way to work together.

When talking about Java, people will immediately think of an object-oriented programming language similar to C ++ that is suitable for distributed environments, think of the features of this language, such as simplicity, stability, security, unrelated to the architecture, portability, interpretability, high performance, multithreading, and dynamic. These are the main features of Java as a programming language.

Java was developed by a technical team of Sun. In the process of implementing the Java language, the Sun Team's technical staff quickly realized that C ++ could not become a fully object-oriented and networked development language. C ++ is developed by adding object-oriented functions to the original C language. Therefore, it has inherent limitations. This is mainly reflected in the wide variety of C ++ features, a large number of redundancy, and no C ++ compiler can support all its functions. In view of this situation, Sun's technical staff decided not to expand C ++, but to develop a new computer language (Oak, the predecessor of Java ). However, C ++ has become a language that most programmers are familiar with. The Java design obviously cannot ignore this reality. If the difference between Java and C ++ is too large, it will undoubtedly take a lot of time and effort for programmers to learn the language. Therefore, Java retains as many C ++ styles as possible.

Since its birth, Java has created numerous small applications on clients for network users. This type of applications have good performance and a large number of results, so many users think of the Java programming language, an incorrect impression appears in my mind-Java is used to write small client programs. In fact, with the advancement of technology, the Java language is gradually changing its image of low execution efficiency, unable to serve as a key computing task for enterprises, and is constantly moving forward to the core of computing technology. Today's Java technology is penetrating various application fields along the network.

Enterprise computing: enterprise computing is the most important technical topic of Java. Sun has released the specification of Enterprise JavaBean (EJB, Enterprise JavaBean), and many companies began to develop Java Technology in the Enterprise application field. IBM has also developed ibm hpcj (High Performance Compiler for Java) 12.0 for Windows NT, and developed ibm jdk (JavaDevelopment Kit) for Windows NT, novell also announced a new server-side Enterprise Java platform, and Sun is actively upgrading its JDK system. This situation shows that Java is steadily moving towards enterprise high-end computing. For Java, the main programming tool competing with other programming languages for enterprise computing has the following advantages: first, when Java is engaged in Object-Oriented Programming, it is simpler than other programming languages such as C ++, so it ensures high programming efficiency and reduces programming investment. Second, the Java Virtual Machine technology provides "one-time programming, the cross-platform capability that is used everywhere is very suitable for the network environment, which provides convenience for Java's development on the network server side. Third, Java has a strong team of providers and supporters, this team includes IBM, Oracle, Novell, Sybase, Netscape, and other companies.

Speed-up operation: many enterprise application developers love the Java language features very much. However, when developing important systems, the choice between language features and execution efficiency is often confusing. In key computing, users may not care about how to compress data or how to set the running delay relationship, but pay great attention to the running speed of the program, this puts Java compilation policy development first. The current Java language does not just explain the execution method. The instant compiler (JITC, just-in-time compiler) technology and prototype compilation technology have been adopted by many manufacturers, technology providers, including Sun, IBM, Oracle, and Netscape, are using these technologies to gradually increase Java execution speed. Among them, IBM has already added the Java Virtual Machine (JVM, JavaVirtual Machine) the operating system and hardware features are organically combined to greatly improve the Java execution efficiency.

Embedded Computing: embedded Java is an application technology with great potential. It gives full play to the small and flexible features of Java. Taking HP as an example, the company manufactures compilation tools and Java virtual machines in its own way to embed Java into various devices, such as printers, medical monitors, and ATMs. Embedded devices rely on a real-time operating system to process an event that takes effect in real time. After Java is embedded into these devices, real-time extension is applied, this gives the device a certain degree of intelligence, enhances the manageability and availability of the embedded device, and greatly improves the work efficiency of the device. Each Vendor attaches great importance to this potential market. Currently, this market lacks a standard. If there is a standard, I believe that a large number of devices embedded in Java will soon be available.

Microsoft's just-released Windows XP gave up its support for Java, but Java can run independently on many operating platforms, including Linux, in addition, it is better to use some features than on Windows. We have reason to abandon Windows and choose Linux for Java development. Now, you can hold Linux in your left hand, Java in your right hand, and then say to Bill Gates with Windows XP in your smile: "Let's hell with your XP! "

For software developers, no programming language can be perfect. If you want to better understand the Java language, the best way is to compare the language with the same type of language. Since Java is similar to C ++, comparing it with C ++ is also a matter of course. Every developer has their own opinions on which one is good and which one is not good. I personally think Java Development is better than C ++. Of course, everyone's views and preferences are different. Later articles will introduce the differences between Java and C ++ and their improvements. Strong or weak, you will naturally understand.

We know that Java uses the C ++ syntax format at the beginning, basically to allow programmers to quickly familiarize themselves with the Java syntax format and shorten the learning time of Java, after all, C and C ++ are still the most common programming languages. However, if we carefully examine the detailed design of the Java program language, we can find that Java removes many C ++ features and adds some new features. These differences with C ++ include:

1. Preprocessor functions such as # define and # include are no longer available

An important feature of C ++ is its Preprocessing Program. Some other programming languages also include the # include function, but still lack the ability to process macros (Macro. # The define function can be replaced by the constant method in Java, but # include is not required in Java, because during Java program execution, the data type is recorded in the object. We do not need to rely on some header files to know the Data Type of the object or value we are using.

If you only use the # include and # define functions of the Preprocessing Program when using the C ++ language, you probably won't feel any troubles caused by the design like Java, however, if you are a master of macro functions in the C ++ Preprocessing Program, you may find it inconvenient and doubt the significance of such a design in Java.

Although preprocessing programs can easily achieve many functions, the maintenance of the entire software is unfavorable from the perspective of software engineering. Since the Preprocessing Program in C ++ is too powerful, a powerful programming Expert often develops a set of macro languages that can only be understood by himself, once the entire software is handed over to others for maintenance, it is difficult for the successor to understand the macro functions written by the previous person in a short period of time, increasing the difficulty of team work and maintenance in the future during software development.

Another point is the program code seen by the C ++ compiler, which is actually different from the program code seen by the program designer. The program designer sees the program code that has not been processed by the Preprocessing Program, and the compiler sees the program code that has been processed by the Preprocessing Program. Once the macro content handed over to the Preprocessing Program for processing is incorrect, the error message generated by the compiler will not be expected by the programmer. This also increases the difficulty of the program in troubleshooting.

The existence of the Preprocessing Program also causes inconvenience in reading the program. If you want to use a C ++ language program that has been completed by someone else, you should not only read the file written by others, but also check the file together to get a full picture of the program. For a Java program, you only need to check the java program file.

2. structure, union, and typedef are no longer available

In fact, as early as C ++, we can remove structure and union in C language and other custom structure types for complex data, because the Class definition method can fully implement this function. Typedef is not necessary. You can use all classes. Although C ++ is designed to be compatible with C, it is not only unnecessary to use redundant language features, but also easy to confuse the program.

3. No more functions

In Java programming language, the most important unit in the language of the program wizard-Function is removed ). If we look at the function as an object wizard, we can understand the concept of the function in the object wizard, it is not necessary. In the program concept of object wizard, object data is the real subject, and the method for processing object data must be attached to the object to make sense. Therefore, removing a function does not mean that there is no module-based procedures such as subprograms. On the contrary, the function is replaced by methods in the object, and the development strategy of the Wizard is strengthened again.

4. Multiplelnheritance)

In C ++, multi-inheritance is a strong function, but it is also difficult for general people to control. Removing multiple inheritance reduces Java functions, but also greatly simplifies the difficulty of writing programs. Although the multi-inheritance function is removed, Java provides the interface method to achieve some multi-inheritance functions. The so-called interface basically defines the prototype of the method for external communication of a class, and the constant number inside the class. Unlike the multi-inheritance, the interface does not define the content of the class method, and variable data in the class.

5. No more Goto

In the history of programming language development, Goto has always been a function of mixed reputation. In many cases, the use of Goto can greatly reduce unnecessary program code, but also because Goto can freely change the process of the program, if you use it rashly, it is more likely to cause program structure confusion. In general, examples of correct use of Goto appear inside the loop and want to end a loop early. In the C language, we can use break or contine to change the process of a certain layer of loop. However, if we want to change the process of two or more layers of loop execution, we can use a boolean instead of a Goto) variable, combined with the judgment of the previous string if-then-else.

On the one hand, Java removes the Goto function, while on the other hand, it also expands the break and continue functions, allowing multi-level cyclic break or continue. This not only avoids the possibility of abusing Goto, but also saves the benefits of Goto.

6. OperatorOverloading is no longer available

In C ++, Operator Overloading is also a design worth discussing. In almost all C ++ books, some examples will be cited to tell you that using OperatorOverloading can make your program look more natural. The following is a complex category customized by a programmer:

// C ++ custom plural class and 0 pemtor Overloading
Class Complex {
Complex (double real, double image ){
Real_number = real;
Image_number = image;
Complex operator + (Complex & rhs ){
Return Complex (rhs. real_number + real_number,
Rhs. image_number + image _, nulnbef );
Doublereal_number // real part
Doublejmage_nunmber; // virtual part

Here, if you use + as the addition symbol of the plural number, there is no doubt, but if you use a symbol like * or, after others see your program, it is difficult to guarantee that there will be no known errors. This is also a major problem with Operator Overloading. When operators are defined by everyone, the readability of the entire program will be greatly affected. The existence of Operator Overloading is not necessary. We can define methods in the class to achieve the same purpose. As for the advantages and disadvantages of removing this function in Java, I am afraid the readers should judge it by themselves.

7. Cancel automatic type conversion

Java is a program language that strictly performs type checks. For the following programs, only warning information is displayed during compilation on the C ++ compiler, but in Java, It is not passed:

Int aInteger; Double aDouble = 2.71828; AInteger = aDouble;

Although such a transformation is legal in C ++, it will also cause loss of data accuracy. To fully understand this, Java's compiler must accept type casting:

Int aInteger;
Doublea-double = 2.71828;
AInteger = (int) aDouble;

8. No pointers

Canceling the data type like Pointer may surprise many programmers who are familiar with the C ++ language. In the C ++ language, the flexible use of pointers is a good choice for many programmers, but the longest time for debugging is also a pointer problem. With C ++'s attitude towards memory management, programmers must track the memory they want from the system, and finally return it to the system. When using pointers, be careful not to cross legal memory space, resulting in problems such as Segmentation Fault or General Protection Fault.

Java removes the pointer type and does not mean that programmers are developing advanced data structures, such as stacks, queues, and binarytrees, in traditional Basic, you must use a wide range of arrays to simulate the system memory, and construct a pointer-like representation.

On the contrary, Java provides a Reference type similar to that in the Lisp language. by reading the configured memory content through the Reference, you can ensure that you do not read your own memory space. On the other hand, the Execution System of the program can also perform memory garbage collection dynamically, and reclaim the memory space not referenced by reference to the system.

9. Connect to C ++

No matter how powerful Java is, someone always needs to connect it with C ++. As soon as a new programming language or software development tool appears, you will ask: "Does it have the ability to connect to the original library? "C ++ plays an important role in the computer industry. As a matter of fact, we directly asked, "Can it be connected to C ++? ". Currently, Java provides methods to connect to C ++, basically, it constructs a dynamic link Library (DLL) for the program written in the C ++ language, and then calls the functions in the DLL by the Java program.

This connection makes the functions in the DLL a "method" from the perspective of Java ". However, because this method is provided directly by other programming languages, rather than written in Java, it is called NativeMethod ).

Due to some security restrictions of Java Applet, this method of connecting to external programs can only be used in Java Application.


In fact, the two statements constant and typedef contain the # define statement. Currently, the structure and Union have been replaced by Java classes. The reason for deleting these features is that, to maintain backward compatibility with the C language, the C ++ language specification contains a large amount of redundancy. For example, the class actually includes the structure and union functions, so the two data structures can be completely canceled. For the # define statement, the Java language standard producer believes that although the starting point of the statement is to enhance the readability of the program, the actual effect is the opposite, which often leads to hard-read code, therefore, it should be canceled. Java no longer supports independent functions, so any function must be encapsulated in a class. Because it is widely believed that the superclasses used by C ++ are very unstable, Java has abandoned the multi-inheritance in C ++ and replaced it with interfaces. Java interfaces refer to the methods that a class can implement in other classes. It only shows the methods, constants, and variables of a class, rather than the full structure of the class.

Finally, Java removes the GOTO statements, Operator overloading, automatic type conversion, and pointer Data Types in C ++. The controversy caused by the GOTO statement has been around for many years, but it is related to some programmers who have a special liking for this statement. C ++ still supports automatic conversion of data types, but Java requires programmers to explicitly convert data types. Automatic data type conversion allows two variables of different data types to assign values to each other without explicit descriptions. This sometimes causes some problems, the most common of which is the loss of precision. For example, if a signed 32-bit integer is assigned to an unsigned integer, all results are positive. Java designers think this can easily cause program errors and decide not to support this conversion method.
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: 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.