In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces the example analysis of JAVA portability, which is very detailed and has certain reference value. Friends who are interested must finish it!
1.JAVA as a programming language: source code portability
As a programming language, JAVA provides one of the simplest and most familiar portability-source code migration. This means that any JAVA program, no matter what CPU, operating system, or JAVA compiler it runs on, will produce the same result. This is not a new concept. People can also have the same effect by using C # and C++. But there are too many options for programming in C or C++, and it is not strictly defined in many details, such as the value of uninitialized variables, access to freed memory, the Mantissa of floating-point operations, and so on. So unless you design strictly according to system-independent concepts in the first place, this portability can only be a theoretical assumption, not a practice. In short, although C and C++ have strict grammatical definitions, their symantics definitions are not standard. This semantic disunity makes the same program produce different results in different system environments. Sometimes even if the system is exactly the same and just because of the different settings of the compiler, it can produce unexpected results. JAVA is different. It defines a strict semantic structure, so that the compiler does not undertake this kind of work. In addition, JAVA defines the behavior of the program more strictly than C and C++, such as: it provides automatic memory collection function (GarbageCollection), so that the program can not access out-of-boundary memory; it provides certain values for uninitialized variables, and so on. These features can reduce the differences between JAVA programs running on different platforms, and make JAVA better platform independent than C and C++ even without the existence of JAVA virtual machine. However, these characteristics also have their disadvantages. JAVA envisions running on computers with 32-bit bytes and 8 bits per byte, which makes it impossible for those 8-bit word-length computers and some supercomputers to run JAVA programs effectively. Only those portable C and C++ programs can be run on such a platform.
2.JAVA as a virtual machine: CPU portability
The object code generated by most compilers can only run on one CPU (such as Intel's x86 series), and even compilers that can support multiple CPU cannot produce object code suitable for multiple CPU at the same time. If you need to run the same program on three CPU, such as x86, SPARC, and MIPS, you must compile it three times.
But the JAVA compiler is different. The object code (J-Code) generated by the JAVA compiler is for a non-existent CPU--JAVA virtual machine (JAVAVirtualMachine), not an actual CPU. The JAVA virtual machine can mask the differences between different CPU, so that J-Code can run on any machine with JAVA virtual machine.
The concept of virtual machines is not unique to JAVA: the University of California proposed the concept of PASCAL virtual machines a few years ago; Perl scripts, which are widely used in Unix servers, also generate machine-independent intermediate code for execution. But what is special about the JAVA virtual machine designed for Internet applications is that it can generate secure object code that is not threatened by viruses. It is because of Internet's special requirements for security features that JVM can be quickly accepted. Nowadays, mainstream operating systems such as OS/2, MacOS and Windows95/NT have provided support for J-Code or very soon.
As a virtual CPU,JAVA virtual machine is independent of the source code (SourceCode). We can generate J-Code not only in Java, but also in Ada95. In fact, there are J-Code compilers for several types of source code, including Basic, Lisp, and Forth. Once the source code is converted to J-Code, the JAVA virtual machine can execute it regardless of which source code it is generated from. The result is CPU portability.
The advantage of compiling the source program to J-Code is that it can run on a variety of machines, while the disadvantage is that it is not as fast as native code.
3.JAVA as a virtual operating system (OS) and graphical user interface (GUI): operating system portability
Even after recompilation, most Windows programs written in C and C++ will not run on Unix or Macintosh systems. Why is that? Because programmers use a large number of WindowsAPI and interrupt calls when writing Windows programs, and the calls to system functions of Windows programs are very different from those of Unix and Macintosh programs, unless the full set of WindowsAPI is transplanted to other operating systems, the recompiled program still can not run.
JAVA solves this problem by providing a set of platform-independent library functions (including AWT, UTIL, LANG, and so on). Just as JVM provides a virtual CPU, JAVA library functions provide a virtual GUI environment. The JAVA program only calls the JAVA library functions, while the library functions call the operating system functions by different virtual machines. JAVA also uses a "rare name" (least-commom-denominator) in its OS/GUI library to provide a function call to a particular operating system, that is, this function is only valid in a particular environment and is ignored in other operating systems. The advantage of this is that an application with a familiar interface can be generated for one operating system while the program can run on other systems. The disadvantage is that some function calls in the system are highly dependent, so it is difficult to implement in the virtual OS/API of JAVA. In this case, programmers can only write programs that are not portable.
The above is all the content of the article "sample Analysis of JAVA portability". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.