Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the basic differences between Java and C++

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the knowledge about "what are the basic differences between Java and C++". In the actual case operation process, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!

We know that Java began to use C++ syntax format, basically to allow programmers to quickly familiarize themselves with Java syntax format, shorten the time to learn Java, after all, C and C++ is still the most people will be a programming language. But if we look closely at the many details of the Java programming language design, we can see that Java has removed many of the features of C++ and added some new features. These differences from C++ include:

1. No longer has #define,#include and other preprocessor functions

One of the most important features of C++ is its preprocessor. Some other programming languages add #include functionality, but lack the ability to handle macros.# The function of define in Java we can use the definition of constants (constant) to replace, and #include in Java is not needed, because in Java when the program is executed, the type data will be recorded in the object entity, we do not need to rely on some header file (header file) to know what data type we use object or value belongs to.

If you only use #include and #define functions of preprocessor when using C++ language, then you probably won't find Java design to cause you any trouble, but if you are a master of using macro functions in C++ language preprocessor, you may find it inconvenient and wonder what Java is designed for.

Although the use of pre-processing procedures can be very convenient to achieve many functions, but from the perspective of software engineering, the maintenance of the entire software is actually very unfavorable. Because the function of preprocessor in C++ language is too powerful, powerful programming experts often develop a macro language that only they can understand. Once the whole software is handed over to others to maintain, it is difficult for successors to understand the macro function written by the previous person in a short time, increasing the difficulty of team work and future maintenance during software development.

Another point is that the C++ compiler sees the program code differently from the program designer. The programmer sees code that has not been preprocessed, and the compiler sees code that has been preprocessed. Once the macro given to the preprocessor is wrong, the compiler will not generate the error message that the programmer expected. This naturally increases the difficulty of debugging.

The presence of the preprocessing program also causes inconvenience to the reading program. If you want to use a C++ program that someone else has written, you usually have to read not only the file he wrote, but also the file to get the whole picture. If it is a Java program, just look at the Java program file.

2. No more structure, union and typedef

In fact, as early as in C++, you can remove the structure and union in the C language for complex data, because the definition of the class (Class) can fully achieve this function. And typedef is also unnecessary, everything can be used with classes. Although C++ is designed to be compatible with C, the use of redundant language features is unnecessary and confusing.

3. No more functions.

In Java programming language, the most important unit in the program wizard language-function (Function) has been removed. If we look at functions in terms of object guides, we can see that functions are unnecessary in the concept of object guides. In the object-oriented program concept, the data of the object is the real subject, and the method of processing the data of the object must be attached to the object to make sense. Therefore, removing functions does not mean that there is no longer the concept of modular programs such as subroutines, on the contrary, it is to replace functions with methods in objects, and once again strengthen the development strategy for wizards.

4. No more multiple inheritance

In C++, multiple inheritance is a strong feature, but it is also a part that is difficult for ordinary people to control. Removing multiple inheritance reduces the power of the Java language, but it also greatly simplifies the difficulty of writing programs. Although multiple inheritance has been removed, Java provides interface methods that can achieve some of the multiple inheritance functionality. The so-called interface basically defines the method prototype of a class's external communication, as well as the constants within the class. The difference between multiple inheritance and interface is that the interface does not define the content of the class method, and the variable data in the class.

5. No more Goto.

Goto has been a feature of mixed reviews throughout the history of programming languages. Goto can be used in many cases to significantly reduce unnecessary code, but also because Goto is free to change the flow of the program, if used rashly, it is more likely to cause chaos in the program structure. In general, examples of the correct use of Goto occur within loops, wanting to end a loop early. In C, we can use break or contine to change the flow of a loop at one level, but if we want to change the execution flow of more than two loops, we can either use Goto or use extra boolean variables, combined with a string of if-then-else judgments.

Java removes Goto functionality on the one hand, and expands break and continue functionality on the other, allowing multi-level loops of break or continue. This not only avoids the possibility of Goto abuse, but also preserves the benefits of Goto.

6. No more OperatorOverloading

In C++, Operator Overloading is also a design worthy of discussion. Almost all C++ books cite examples of how using Operator Overloading can make your programs look more natural. For example, the following is a programmer's custom plural class:

//C++ custom complex classes and 0pemtor Overloading

class Complex{

public:

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);

}

private:

doublereal_number //real part

doublejmage_nunbber; //imaginary part

}

Here, if you use + as the addition symbol of the complex number, no one will have any doubts, but if you use the symbol * or>, then people will see your program, and there is no guarantee that there will be no cognitive error. This is also a big problem with Operator Overloading, when everyone gives their own definition to the operator, the readability of the whole program will be greatly affected. Operator Overloading is not necessary, we can also define methods in the class to achieve the same purpose, as Java to remove the advantages and disadvantages of this feature, I am afraid it is up to the reader to judge.

7. Cancel automatic type conversion

Java is a strict type-checking programming language. For the following programs, only warning messages will appear at most when compiled on C++ compilers, but they will not pass in Java:

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

Although such a transformation is legal in C++, it also results in a loss of data accuracy. Java In order to make sure that the programmer understands this fully, it must be type casting for Java compilers to accept:

int aInteger;

doublea Double=2.71828;

aInteger=(int)aDouble;

8. No more pointers.

Eliminating a data type like Pointer may come as a surprise to many programmers familiar with C++. In C++, the flexible use of pointers is the pride of many programmers, but it is also the pointer problem that accounts for the longest debugging time. In line with C++'s attitude towards memory management, programmers must track the memory they ask for from the system and return it to the system, and be careful not to cross legal memory space when using pointers, causing problems such as Segment Fault or General Protection Fault.

Java removes pointer types, which does not mean that programmers who develop high-level data structures such as stacks, queues, and binary trees must use a wide range of arrays to simulate system memory and construct pointer-like representations themselves, as in traditional Basic.

On the contrary, Java provides a Reference type similar to Lisp, which reads the configured memory content through Reference, ensuring that it does not read memory space that does not belong to itself. On the other hand, the execution system of the program can also dynamically do memory garbage collection, and the memory space that has not been referenced by reference can be recycled to the system for use.

9. and C++ connections

No matter how powerful Java is, someone always needs to connect it to C++. Because as soon as a new programming language or software development tool comes along, people ask,"Does it have the ability to connect with existing libraries?" C++ is a very important language in the computer world. The question you're asking is basically,"Can it connect to C++?" "。At present, Java does provide a method to connect with C++ language, which basically constructs programs written in C++ language into Dynamic Linking Library (DLL), and then Java programs call functions in DLL.

This concatenation makes a function in DLL look like a "method" from Java's perspective. However, because this method is directly provided by other programming languages, rather than written in Java, it is called a "Native Method."

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

What are the basic differences between Java and C++? Thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report