First, what is facing the object? What is process orientation?

1. The difference between object – and process-oriented

Process-oriented: analyze the steps needed to solve the problem, and then use functions to implement these steps step by step, using the time one by one call on the line; Object orientation: The decomposition of the things that constitute the problem into multiple objects, the purpose of building objects is to describe the behavior of a thing in the whole step of solving the problem.

There are two ways to implement backgammon:

  1. The first step of the process-oriented design idea is to analyze the steps of the problem: ① start the game, ② black drop chess, ③ draw the picture, ④ judge the win or lose, ⑤ white drop chess, ⑥ draw the picture, ⑦ judge the win or lose, ⑧ return to steps ② and ⑨ output the final result;
  2. The object-oriented design idea is to split objects first: ① the player character (black and white), ② the board system (drawing pictures), ③ the rule system (judging winners and losers and violations);

From the above design ideas: object oriented to function to divide the problem, not steps. As with drawing, this behavior is spread over multiple steps in process-oriented design, and then it is likely that there will be different versions of drawing, because often the designer will simplify them for practical purposes. But in object-oriented design, drawing only appears in the checkerboard object, other objects do not need to determine how to implement it, thus ensuring the unity of drawing.

In general, object-oriented is the abstraction of things, process oriented is a top-down, step-by-step refinement of programming.

2. Characteristics of objects:

The idea of object orientation is to treat everything as an object, and objects are made up of properties and methods. Properties are static characteristics of objects, methods are dynamic behavior of objects. For example, age and weight are his characteristics, while eating and sleeping are his behaviors. We call the properties and methods of an object object members.

Class: A Class is a template that describes a Class of objects that have the same properties and methods.

Person jack = new Person();
Person lucy = new Person();
Copy the code

As above: Person is a class, while Jack and Lucy are instantiated objects. Java instantiates with new.

3. Advantages and disadvantages of process and object orientation

A process-oriented program is like an egg fried rice. The eggs and rice are mixed together, and if you want to replace the eggs with vegetables, you have to recook them. Object-oriented programming is like rice with toppings. You have all kinds of rice and dishes ready, and you just have to match whatever you want.

So which of the two is better, have to be analyzed on a case-by-case basis, different scenarios, advantages will become disadvantages, and vice versa.

Summary of advantages and disadvantages:

Process-oriented:

Advantages: Performance is better than object-oriented, because class invocation needs to be instantiated, which costs a lot and consumes resources. For example, embedded development, Linux/Unix, etc., generally adopt process-oriented development, and performance is their primary consideration. Disadvantages: Compared with object-oriented – poor maintainability, low scalability, high coupling degree.

Object-oriented:

Advantages: good maintainability, high expansibility, low coupling degree. Because of the characteristics of object – oriented encapsulation, inheritance and polymorphism, a low coupling system can be designed to make the system more flexible and easier to maintain. Disadvantages: Poor performance compared to process oriented.

Why is Java called “compile once, run anywhere”?

This statement fully reflects the cross-platform nature of Java, and the “compile once, run anywhere” is possible because of the existence of the JVM (Java Virtual Machine).

The computer can only recognize 0/1 machine instructions, and machine instructions on different platforms (systems) may have different meanings, so C, C++ and other languages on different platforms may need to be recompiled.

Java source code has a.java extension, and the compiler compiles Java source files into.class bytecode files, while the JVM is responsible for translating.class files into machine-readable instructions.

Java is actually programmed for JVMS. You simply install the corresponding JVMS on different platforms, and the Java files are compiled to run on different platforms.

Conclusion:

1. JVMS mask differences between operating systems, but JVMS vary from operating system to operating system. Java programs are only responsible for the JVM. JVM executables are.class files.

What are the benefits of Java?

  • Cross-platform (JVM)
  • Object oriented (with encapsulation, inheritance, polymorphism, etc.)
  • Simple (automatic memory management, garbage collection mechanism)
  • Robust (Java is a strongly typed language)
  • Multithreading (restricted by the operating system, if the system does not support, can not be implemented)

Thread is a new concept in operating system. It is also called lightweight process, which is a smaller unit of concurrent execution than traditional process. C and C++ use a single-threaded architecture, while Java provides multithreading support. Java supports multithreading in two ways: On the one hand, the Java environment is inherently multithreaded. Several system threads run for the necessary system level operations such as garbage collection, system maintenance, etc. On the other hand, the Java language has built-in multithreaded controls that can greatly simplify multithreaded application development. Java provides a class called Thread that is responsible for starting a Thread, terminating it, and checking its status. Java threads also include a set of synchronization primitives. These primitives are responsible for concurrency control over threads. Using Java multithreaded programming interface, developers can easily write applications that support multithreading and improve the efficiency of program execution. It is important to note that Java’s multithreading support is somewhat limited by the runtime support platform. For example, the multithreading nature of Java may not manifest itself if the operating system itself does not support multithreading.

  • Distributed (data distribution: can be distributed on different hosts, operation distribution: a calculation is distributed on different hosts)

Java supports the WWW client/server computing pattern, so it supports both distributions. For the former, Java provides an object called a URL, with which you can open and access objects at the same URL address in the same way you access the local file system. For the latter, Java applets can be downloaded from the server to the client, that is, part of the calculation is performed on the client, improving the system execution efficiency. Java provides a complete set of network class library, the developers can use the library for network programming, convenient implementation of Java distributed characteristics.

  • Reliability and safety

Java was originally designed for use in consumer electronics products and therefore requires high reliability. Java, although derived from C++, eliminates many C++ unreliability factors and prevents many programming errors. First, Java is a strongly typed language that requires explicit method declarations, which ensures that the compiler can detect method invocation errors and that programs are more reliable. Second, Java does not support Pointers, which prevents unauthorized access to memory. Third, Java’s automatic cell collection prevents problems caused by dynamic memory allocation such as memory loss. Fourth, the Java interpreter runtime implements checks that can detect out-of-bounds array and string access; Finally, Java provides an exception handling mechanism that allows programmers to put a group of error codes in one place, which simplifies error handling and facilitates recovery. Because Java is mainly used for network application development, it has high requirements on security. Without a security guarantee, it is very dangerous for users to download and execute programs from the Internet. Java uses its own security mechanism to prevent the generation of virus programs and download programs from damaging the local system. When Java bytecode enters the interpreter, it must first be checked by the bytecode verifier. The Java interpreter then determines the memory layout of the classes in the program. The class loader then takes care of loading the classes from the network into a separate memory area, avoiding interference between applications. Finally, client users can restrict classes loaded from the network to access only certain file systems. The combination of these mechanisms makes Java a safe programming language.