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's the difference between Java and C?

2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces the relevant knowledge of "what's the difference between Java language and C language". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. An overview of Java language

As an object-oriented programming language, Java is very similar to C++, but much simpler than C++. It not only integrates the characteristics and advantages of other languages, but also has its own unique advantages.

The main features of Java are as follows:

(1) simplicity. Java can automatically collect garbage generated in memory, which greatly reduces the complexity of the program. In addition, Java adds more practical functions, which makes program development more simple and reliable.

(2) platform independence. In the process of programming, Java language is first compiled into intermediate code, then loaded and checked, and finally executed by different machine codes translated. Therefore, as long as you can support Java virtual machines, you can run a variety of Java programs. This is why Java is the most attractive and easy to use.

(3) object-oriented technology. In recent years, object-oriented technology is most widely used in software development. Object-oriented technology refers to the technology that is programmed in a more intuitive and acceptable way in the programming process. The dynamic networking programming feature of Java maximizes the advantages of object-oriented.

2. Overview of C language

C language, as a new programming language, combines the advantages of assembly language and high-level language, which makes its application more simple and popular. Nowadays, C language has been used in various types of microcomputers. When writing a program, C language can program the program directly. Therefore, C language is widely used and has strong data processing ability. The application of C language is not only used in software development, all kinds of scientific research need to use C language.

The advantages of C language: 1. Is simple and compact, flexible and convenient; 2. Rich in operators; 3. Rich in data types; 4. The expression is flexible and versatile; 5. The generated target code is of high quality, the program execution efficiency is high, and the portability is high.

Because the C language is to stack the same data together, which makes the C language data have great security defects, in the event of an accident, all the data will be lost.

In addition, the C language is relatively lenient in syntax restrictions and variable types, which will affect the security of the program. And C language is more difficult than other high-level languages, so it is not easy to master it completely.

3. Comparison of C and java compilers

The comparison between the compilers of Java and CumberCraft + actually represents the comparison between the most classic JIT compiler and the static compiler.

Note: JIT compiler, Just-In-Time Compiler in English, meaning just-in-time compiler in Chinese. The JIT compiler can compile MSIL into different machine codes to adapt to the corresponding system platform, and finally make the program run smoothly in the target system. Static compilation means that when compiling an executable file, the compiler extracts the part of the corresponding dynamic link library (.so or .lib) that the executable file needs to call and links it to the executable file, so that the executable file does not depend on the dynamic link library at run time. )

To a large extent, it also determines the result of the performance comparison between Java and CumberCraft +.

Because the native machine code is finally executed by the machine after compilation, no matter whether it is API Cleavage + or Java code, which language has better performance, the rest of the comparison becomes a game of "spelling compiler" and "spelling out code quality", except for the implementation of their own Cmax library.

Of course, this comparison also removes the one-sided comparison of development efficiency, the question of which language is better or worse, who is fast and who is slow is a difficult debate, so let's get back to the point and take a look at the advantages of the compilers of the two languages.

The reasons are as follows:

The main results are as follows: (1) because the JIT compiler takes up the running time of the user program, it has a lot of time pressure, and the optimization means it can provide are also seriously restricted by the compilation cost. If the compilation speed can not meet the requirements, the user will detect a significant delay in starting the program or some part of the program, which makes the JIT compiler dare not introduce large-scale optimization techniques casually, and the time cost of compilation is not the main concern in the static optimization compiler.

(2) the Java language is a dynamic type-safe language, which means that a virtual machine is needed to ensure that programs do not violate language semantics or access unstructured memory. At the implementation level, this means that virtual machines must check dynamically frequently, such as checking null pointers when object instances are accessed, checking upper and lower bounds when array elements are accessed, checking inheritance relationships during type conversion, and so on. For the check behavior that is not explicitly written by this kind of program code, although the compiler will try to optimize it, it still consumes a lot of running time on the whole.

(3) although there is no virutal keyword in the Java language, the frequency of using virtual methods is much higher than that of the C-hand C + language, which means that the frequency of polymorphic selection of the method receiver at runtime is much higher than that of the C-hand C + language. it also means that the JIT compiler makes some optimizations, such as method inlining, which is much more difficult than the static optimization compiler.

(4) Java is a language that can be extended dynamically, loading new classes at runtime may change the inheritance relationship of program types, which makes many global optimizations difficult, because the compiler can not see the full picture of the program, and many global optimization measures can only be completed in a radical optimization way. The compiler has to pay attention to it all the time and undo or re-optimize as the type changes.

(5) the object memory allocation in Java language is carried out on the heap, and only the local variables in the method are allocated on the stack. On the other hand, there are many kinds of memory allocation methods for the objects of Cracket +, which can be allocated either on the heap or on the stack. If the objects owned by the thread can be allocated on the stack, the pressure of memory recovery can be reduced, and the memory barrier does not need to be considered. In addition, the allocated memory is mainly reclaimed by the user program code, so there is no process of filtering useless objects, so the garbage collection mechanism is also high in terms of efficiency (only refers to running efficiency, excluding development efficiency).

(6) because C is compiled, the source code is directly compiled into machine code, while JAVA is interpreted, and the source code is compiled into binary pseudocode, which is interpreted and executed by the JAVA virtual machine. However, because C is compiled, its portability is poor, while JAVA is interpreted and executed, so it has good portability and can run across platforms.

At the same time: compiling an ordinary local application program, generally c is faster than java, compiling web application, because the cgi program realized by c is basically process, and the management servlet of java application server is threaded, so in the case of large traffic, java has an advantage.

(finally) there are a lot of disadvantages of Java language relative to Java Candle +, but it's not that Java is really not as good as it is. I believe you have noticed that these performance disadvantages of Java language are the price paid in exchange for the advantages of development efficiency. Dynamic security, dynamic extension, garbage collection and these "slow-down" features have made a great contribution to the development efficiency of Java language. Moreover, it may not be possible that there is no JIT compiler for Java to do, while the static optimization compiler for Cmax + cannot: because of its static nature, it cannot carry out optimization measures based on run-time performance monitoring, such as call frequency prediction (Call Frequency Prediction), branch frequency prediction (Branch Frequency Prediction), clipping unselected branches (Untaken Branch Pruning), etc. All these will form some unique performance advantages of the Java language.

4. The influencing factors of the slowness of the Java program compared with that of the CCompact + program.

1) the inherent overhead of interpretive language: at run time, the java program class loader loads the relevant classes from the class path, and then the java virtual machine reads the bytes of this kind of file and performs the corresponding operations. When C compiles, the program is compiled into local machine code. Generally speaking, the execution speed of java program is 10-30 times slower than C. Even using just-in-time compiling (reading class file bytes and compiling to local machine code) technology, the speed is much slower than C.

2) bytecode loading and execution overhead: java programs load class bytes from the network and then execute them, which is also the reason why java runs slowly.

3) Runtime overflow detection overhead: in the process of running the program, the java virtual machine detects whether the array is out of bounds, but not in C.

4) the difference between heap and stack: all objects in java are created in heap, no objects are created in stack, while some objects and variables in C are created in stack.

5) Runtime reference detection overhead: java detects whether the reference to the object is null or not, if the reference points to both null pointers, and throws a null pointer exception when a method is executed

6) Runtime type detection overhead: the java runtime detects types and throws a ClassCastException exception if the type is incorrect.

7) huge overhead of GC: java's garbage collection mechanism is less efficient than C's memory managed by programmers.

8) Type conversion overhead: the length of the original data types in java is the same in each operating system platform, while the length of these data types in C varies with different operating systems, so there is a conversion process when java is executed on different operating systems.

9) String type overhead: in java, String is UNICODE. When java wants to operate an ASCII string, it is relatively less efficient than C.

10) dynamic link cost: dynamic link is used in java

This is the end of the content of "what's the difference between Java language and C language". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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