Java Virtual Machine (JVM) and Cross-Platform Principles <br />I believe everyone has learned that Java has cross-platform features, can be "compiled at once and run everywhere". Programs written under Windows can be used in Linux without any modifications. Run, this is hard to do with C and C++.
So, how is cross-platform implemented? This involves talking about Java Virtual Machine (JVM).
JVM is also a software, and different platforms have different versions. The Java source code we write will generate a .class file called a bytecode file after compilation. The Java virtual machine is responsible for translating the bytecode file into machine code under a specific platform and then running it. In other words, as long as the corresponding JVM is installed on different platforms, you can run the bytecode file and run the Java program we wrote.
During this process, the Java program we wrote did not make any changes. It can be run on different platforms just through the "middle layer" of JVM, which truly achieves the purpose of "compiling one time and running everywhere".
JVM is a "bridge" and a "middleware" that is the key to cross-platform implementation. Java code is first compiled into bytecode files, and then the JVM translates the bytecode files into machine language, thereby achieving the running of Java programs. purpose.
Note: The compilation result is not to generate machine code, but to generate bytecode. The bytecode cannot be run directly and must be translated into machine code through JVM before it can be run. The bytecode generated by compilation and generation under different platforms is the same, but the machine code translated by JVM is different.
Therefore, running Java programs must have JVM support, because the compilation result is not machine code, and it must be translated again by the JVM before execution. Even if you package Java programs into executables (such as .exe), you still need JVM support.
Note: Cross-platform Java programs, not JVMs. JVM is developed in C/C++, and is compiled machine code. It cannot cross-platform. Different versions of JVM need to be installed under different platforms.
About the execution efficiency of JVM
In the past few years of Java launch, people had different opinions. Explaining that bytecode is definitely much slower than running machine code at full speed. Is it worth sacrificing performance for cross-platform advantages?
However, the JVM has an option to translate and save the most frequently used bytecode into machine code, a process known as instant compilation. This method is indeed very effective, which has led to the use of virtual machines on Microsoft's .NET platform.
Timely compilers are now quite outstanding, and have even become competitors of traditional compilers, and in some cases even surpass traditional compilers because the JVM can monitor runtime information. For example, an instant compiler can monitor and optimize code that is frequently used, eliminating function calls (i.e. "embedded").
However, Java has some additional overhead that C/C++ does not have, and the key applications are slower. For example, Java adopts a platform-independent drawing method, and the execution of GUI programs (client programs) is slow; it also takes time to start a virtual machine.
The failure of the client market
Java's GUI library is not excellent, the interface is not friendly, and most users are not used to it; Java's client resources are also consumed relatively large, and the performance of large data applications and complex functions is worrying.
What is even more unacceptable is that after Microsoft splits its own interests and SUN, Windows no longer has pre-installed JVM. Before users install your program, they must install JVM and set it up correctly. You can ask ordinary users to install your software. But can you expect him to know about JVM and install the settings correctly?
Although you can integrate the JVM into your program, automatically install and set it up, and not allow user intervention, do you want to come with a JVM that is much larger than your program? It may be acceptable for a software to do this. Thousands of software do this. How many JVMs do users need to install? How much is disk space wasted?
Therefore, few client programs for ordinary users who are directly launched on the market are developed in Java. Most of the clients developed in Java are used by internal employees of the enterprise. When employees receive the computer, the technical department has configured them. If you want to engage in client development, it is recommended to learn C/C++ and .NET, which have great advantages in Window client development.
Various reasons are destined to be unfavorable to be introduced to the market and are accepted by ordinary users. But then again, client development is not the original intention of Java. Java was originally aimed at embedded, but it grew rapidly with the rise of the Internet and showed its strength in web development.
Java classes and objects concepts
Java is an object-oriented programming language. To understand Java, you must first understand the concepts of classes and objects.
Classes in Java can be regarded as an upgraded version of structures in C language. A structure is a constructed data type that can contain different members (variables), and the data types of each member can be different; structure variables can be defined through structures, and each variable has the same properties. For example:
#include <stdio.h>int main(){ // Define the structure Student struct Student{ // The variable char *name; int age; float score; }; // Define the variable struct Student through the structure stu1; // Members of the operation structure stu1.name = "Xiao Ming"; stu1.age = 15; stu1.score = 92.5; printf("%s' age is %d, and the score is %f/n", stu1. name, stu1.age, stu1.score); return 0;} Running results:
Xiao Ming's age is 15, and his grade is 92.500000
Classes in Java are also a type of constructing data types, but have been extended. The members of the class can not only be variables, but also functions; variables defined through classes also have specific names, called "objects". For example:
public class Demo { public static void main(String[] args){ // Define class Student class Student{ // Define class through class keyword class// Variables contained in the class String name; int age; float score ; // Class The included function void says(){ System.out.println( name + "The age is " + age + ", the score is " + score ); } } // Define variables through a class, that is, create an object Student stu1 = new Student(); // The new keyword must be used// The member of the operation class stu1.name = "Xiao Ming"; stu1.age = 15; stu1.score = 92.5f; stu1.say(); }} Running results:
Xiao Ming's age is 15 and his grade is 92.5
In C language, the definition of structure variables can be completed by the structure name and memory space can be allocated; however, in Java, defining variables through classes only does not allocate memory space, and the new keyword must be used to complete the memory space. distribute.
Analogy can be compared to drawings, objects can be compared to parts, and the drawings explain the parameters of the parts and the tasks they undertake; a drawing can produce parts with the same properties, and different drawings can produce different types of parts.
In Java, using the new keyword, you can create objects through classes, that is, producing drawings into parts. This process is called instantiation of classes, so the object is also called an instance of a class.
Note: The class is just a drawing that plays an explanation and does not occupy memory space; objects are specific parts, and they must have a place to store them to occupy memory space.
Variables and functions contained in a class have specific names. Variables are called attributes (usually also called member variables), and functions are called methods, attributes and methods are collectively called members of a class.
Object Oriented Programming (OOP)
Classes are a general concept. Many programming languages such as Java, C++, C#, and PHP have classes, and objects can be created through classes. Classes can be regarded as an upgraded version of structures. The younger generations of C language saw the shortcomings of C language, tried to improve them, inherited the idea of structures, and upgraded them to allow programmers to develop or expand large and medium-sized projects. It's easier.
Because Java, C++ and other languages support classes and objects, writing programs in these languages is also called object-oriented programming, and these languages are also called object-oriented programming languages. Because the C language does not support the concepts of classes and objects, it is called a process-oriented programming language.
In fact, object-oriented is just a process-oriented upgrade.
In C language, the reused code blocks that complete a certain function can be defined as functions, and functions with a class of functions are declared in a header file, and functions of different types are declared in different header files to perform functions Better management, easy to write and call.
In Java, the code block that completes a certain function can be defined as a method, and methods with similar functions can be defined in a class, that is, in a source file (because a source file can only contain one public class) , Multiple source files can be located in a folder, which has a specific name called a package.
The above is C++, while Java is:
Object-oriented programming has absolutely no advantage in software execution efficiency. Its main purpose is to facilitate programmers to organize and manage code, quickly sort out programming ideas, and bring innovations in programming ideas.