Parsing Java JVM and the concepts of classes and objects _java

Source: Internet
Author: User
Tags types of functions advantage

Java Virtual Machine (JVM) and cross-platform principles
I believe you have learned that Java has cross-platform features, you can "compile, run Everywhere", the program written under Windows, without any modification can be run under Linux, this is C and C + + difficult to do.

So how does cross-platform happen? This is about Java virtual machines (Java Machine, referred to as JVMs).

The JVM is also a software, and different platforms have different versions. We write Java source code, compiled will generate a. class file, called a bytecode file. The Java Virtual machine is responsible for translating bytecode files into the machine code under a specific platform and then running. That is, as long as the corresponding JVM is installed on different platforms, you can run bytecode files and run the Java programs that we write.

And in this process, we write the Java program did not make any changes, just through the JVM this "middle layer", can be run on different platforms, really realize the "compile, run everywhere" purpose.

JVM is a "bridge", is a "middleware", is the key to achieve Cross-platform, Java code is first compiled into bytecode files, and then the JVM to translate bytecode files into machine language, so as to run Java program.

Note: The result of the compilation is not to generate machine code, but to generate bytecode, bytecode can not run directly, the JVM must be translated into machine code to run. The bytecode generated under different platforms is the same, but the machine code translated by the JVM is not the same.

Therefore, running the Java program must have the support of the JVM, because the result of the compilation is not machine code, it must be translated by the JVM again to execute. Even if you package Java programs into executable files (such as. exe), you still need JVM support.

Note: A cross-platform Java program, not a JVM. The JVM is developed with C + +, is compiled machine code, not cross-platform, different platforms need to install different versions of the JVM.

About the efficiency of the JVM's execution

Java launched a few years ago, people have different views, the interpretation of bytecode is certainly faster than the full speed of running machine code much slower, sacrificing performance for cross-platform advantage is worth it?

However, the JVM has an option to translate the most frequently used bytecode into machine code and save it, a process called Just-in-time compilation. This is a really effective way to make Microsoft's. NET platform use virtual machines.

The Just-in-time compilers are now quite good, even as competitors to traditional compilers, and in some cases even more than traditional compilers because the JVM can monitor run-time information. For example, the Just-in-time compiler can monitor and optimize the use of high frequency code, eliminating function calls (that is, "inline").

However, after all, Java has some of the additional overhead of C/C + +, and critical applications are slower. For example, Java uses a platform-independent mapping, GUI programs (client programs) to perform slowly, virtual machine startup also takes time.
The folding of the client market

Java GUI Library is not good, the interface is not friendly, most users are not very accustomed to, Java client resource consumption is also relatively large, large data applications and complex application performance is worrying.

Even more unacceptable is that after Microsoft's own interests and sun split, Windows will no longer pre-installed the JVM, the user installed your program, you must install the JVM and the correct settings, you can ask ordinary users to install your software, but you can expect him to understand the knowledge of the JVM and install the settings correctly?

While you can integrate the JVM into your program, install it automatically and set it up without user intervention, do you want to include a JVM that is much larger than your program? One software might be acceptable, thousands of software would do that, and how many JVMs would a user install? How much disk space to waste?

Therefore, direct market-oriented client programs for ordinary users, with little Java development, most of the Java development of the client is for the internal staff to use, staff to the computer, the technical department has been configured well. If you want to engage in client-side development, it is recommended that you study C + + and. NET, which have a greater advantage in window client development.

Various reasons, doomed to the Java client is not conducive to the market, so that ordinary users accept. But then again, client development is not Java's original intention, Java was originally oriented to embedded, but with the rise of the internet and rapid growth, in the web development.

Concepts of Java classes and objects
Java is an object-oriented programming language, Understanding Java, first of all to understand the concepts of classes and objects.

A class in Java can be viewed as an upgraded version of the structure in C language. A struct is a constructed data type that can contain different members (variables), each of which can be of varying data types, and can be defined by a struct body variable, each variable having the same property. For example:

#include <stdio.h>
int main () {
  //define structure body Student
  struct student{//
    struct Body contains variable
    char *name;
    int age;
    float score;
  };
  The variable struct Student stu1 is defined by the structure body
  ;
  The member stu1.name of the operation structure body
  = "xiaoming";
  Stu1.age =;
  Stu1.score = 92.5;
 
  printf ("%s is of age%d, score is%f\n", Stu1.name, Stu1.age, stu1.score);
  return 0;
}

Run Result:

Xiaoming's age is 15, and his grade is 92.500000.

A class in Java is also a constructed data type, but with some extensions, the members of a class can be not only variables, but also functions, and variables defined by the class have a specific salutation, called "Objects." For example:

public class Demo {public
  static void Main (string[] args) {
    //define Class Student class
    student{//define class through class keyword classes 
   //class contains variable
      String name;
      int age;
      float score;
      class contains functions
      void Say () {
        System.out.println (name +) is "+ Age +" and the result is "+ score";
      }
    Define a variable through a class, that is, create an object
    Student stu1 = new Student ();///You must use the New keyword
    //action class member
    Stu1.name = "Xiaoming";
    Stu1.age =;
    Stu1.score = 92.5f;
    Stu1.say ();
  }

Run Result:

Xiaoming's age is 15, and his grade is 92.5.

In c language, the definition of structure variables can be completed by the name of the structure body, and the memory space is allocated; But in Java, only the class to define the variable does not allocate memory space, you must use the New keyword to complete the allocation of memory space.

The class can be likened to a drawing, the object is likened to a part, and the drawing illustrates the parameters of the part and the tasks it undertakes; a drawing can produce parts of the same nature, and different drawings can produce different types of parts.

In Java, using the New keyword, you can create an object from a class, and the drawing is made into a part, which is called an instantiation of the class, and therefore an object is an instance of the class.

Note: Class is only a drawing, play a role in the description, do not occupy the memory space, the object is the specific parts, to have a place to store, will occupy memory space.

A class contains variables and functions that have a specific salutation, a variable called a property (usually called a member variable), a function called a method, and a property and a method are collectively referred to as members of a class.
Object-oriented programming (Object Oriented programming, OOP)

Class is a common concept, in Java, C + +, C #, PHP and many other programming languages have classes, can create objects through the class. Class can be seen as an upgraded version of the structure, the C language of the younger generation to see the lack of C language, to try to improve, inherit the idea of the structure, and to upgrade, so that programmers in the development or expansion of large and medium-sized projects easier.

Because languages such as Java and C + + support classes and objects, writing programs using these languages is also called object-oriented programming, and these languages are also called object-oriented programming languages. C language is called a process-oriented programming language because it does not support the concepts of classes and objects.

In fact, object-oriented is just a process-oriented upgrade.

In C, the reusable block of code that completes a function can be defined as a function, and a function of one function is declared in a header file, and different types of functions are declared in different headers in order to better manage the function for easy writing and invocation.

In Java, you can define a code block that completes a function as a method, defining a method with similar functionality in a class is defined in a source file (because a source file can contain only one common class), and multiple source files can be located in a folder with a specific salutation called a package.

The above is C + +, while Java is:

Object-oriented programming has absolutely no advantage in the efficiency of software execution, its main purpose is to facilitate the programmer to organize and manage the code, to quickly comb the programming ideas and bring about the innovation of the programming ideology.

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: info-contact@alibabacloud.com 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.