2021 latest Java learning summary---Java language features

Java language is simple

The grammar of the Java language is very close to the C language and C++ language. In a sense, it evolved from C and C++, making it easy for most programmers to learn and use .

For C++, it has been simplified and improved to some extent, such as: using interfaces instead of complex multiple inheritance and canceling pointers , and also through the realization of automatic garbage collection mechanism , greatly simplifying the programmer's resource release management work.

Provides a wealth of class libraries and API documents , as well as third-party development kit toolkits , as well as a large number of Java-based open source projects to help programmers learn from it. JDK is one of the open source codes. Readers can analyze the project Source code to improve your programming level.

Java language is object-oriented

Object-oriented is the foundation of the Java language and an important feature of the Java language. Object-oriented refers to the object as the basic unit, including attributes and methods. Object ofState is expressed by attributes, Of the objectBehave in a way

Object-oriented technology makes application development simple and easy to use, saving code.In short, the Java language is a pure object-oriented programming language

Java language is distributed

Distribution mainly includes two major aspects, namely operation distribution and data distribution

Operation distribution refers to the arrangement of related operations on multiple different hosts. Data distribution is to store data on different hosts in multiple networks, using URL (Uniform Resource Locator) to access network objects, access methods and Access to the local system is similar.

The Java language has a powerful, easy-to-use network capability, which is very suitable for distributed computing programs.
Java language supports the development of Internet applications. There is a network application programming interface (java.net) in the basic Java application programming interface, which provides a class library for network application programming, including URL, URLConnection, Socket, ServerSocket, etc. Java's RMI (Remote Method Activation) mechanism is also an important means of developing distributed applications.

<<An easy-to-understand explanation of what is distributed in Java>> link

Java language is multi-threaded

Multithreading mechanism enablesThe application executes multiple tasks in parallel at the same time, Java language provides a synchronization mechanism between multiple threads . These corresponding synchronization mechanisms can ensure that different threads can share data correctly. The multi-threading mechanism makes the program have better interactivity and real-time .

Java language is high performance

Java is a language that is compiled and then interpreted, so it is not as fast as a fully compiled language. However, in some cases, performance is very important. To support these situations, Java designers have created a "just-in-time" compiler that can translate Java bytecode into machine code for a specific CPU (central processing unit) at runtime. It is to achieve full compilation. The Java bytecode format is designed to take into account the needs of these "just in time" compilers, so the process of generating machine code is quite simple, and it can produce quite good code.
Compared with those interpreted high-level scripting languages, Java is indeed high-performance. In fact, the running speed of Java is getting closer and closer to C++ with the development of JIT (Just-In-Time) compiler technology.

Java language is cross-platform

The so-called cross-platform isMeans that the software can run normally in any computer environment without being restricted by computer hardware and operating system. This is the trend of software development and the goal pursued by programmers. The reason for saying this is because there are many types of computer hardware and different operating systems. Different users and companies have their own different computer environment preferences. In order for the software to run normally in these different environments, it needs to be independent of These platforms.
In the Java language, the virtual machine that comes with Java achieves cross-platform performance well. After Java source code is compiled, the binary bytecode generated is platform-independent. When the Java virtual machine executes the bytecode, it interprets the bytecode into machine instructions for execution on the specific platform. This is why Java can Write once, run anywhere (compile once, run anywhere). The Java virtual machine provides a barrier from bytecode to the underlying hardware platform and operating system, making the Java language cross-platform.

Java language is portable

Portability comes from cross-platform, in addition, Java also strictly regulates the length of each basic data type. The Java system itself also has strong portability. The Java compiler is implemented in Java, and the Java operating environment is implemented in ANSI C.

Java language is safe

In the Java language, the pointer and content release syntax similar to the C language is deleted, which effectively avoids illegal operation of the memory. The code can only be run after verification, so it is impossible for unauthorized Java programs to damage the system platform. The biggest advantage of the behavior of Java is that Java can write antivirus and repairable systems . Java is usually used in network environments. For this reason, Java provides a security mechanism to prevent malicious code attacks, which can improve the security of the system. Sex.

Why is it safe? Let's start with its mechanism! !
The compiled .java file will be loaded and verified by the JVM class loader. In the verification process, in addition to verifying whether it meets the compilation standards, it will also verify whether there is any code for operating system files in the code. If so, It may not let you pass the verification. Then, let’s talk about jvm. Jvm is an independent memory area opened up in the system. When your code runs, it will be in a separate sandbox (what is the sandbox? I will add it later...) anyway. In this area, you are also not allowed to access other areas outside the block of memory. For example, if you want to access files on the computer disk through code, it is impossible.

Java language is robust

1. The Java language is a strongly typed language, that is, a large number of type checks are performed during compilation and runtime to prevent the occurrence of unmatched data types.
2. The Java language is designed with automatic garbage collection function to prevent memory allocation errors.
3. Java language has designed an exception handling mechanism

The strong typing mechanism, exception handling, and automatic garbage collection of the Java language are important guarantees for the robustness of Java programs. The discarding of pointers is a wise choice for Java.Java's security check mechanism makes Java more robust

Java language is dynamic

Java is essentially a static language, not a dynamic language. The remarkable feature of dynamic languages ​​is that they can change the program structure or variable types when the program is running. Typical dynamic languages ​​include Python, ruby, javascript, etc.Java is not a dynamic language, but Java has a certain degree of dynamics,
Manifested in the following aspects:

  • Reflection mechanism
  • Dynamic bytecode operation;
  • Dynamic compilation;
  • Execute other script code;

1. Reflection mechanism

The reflection mechanism refers to classes that can be loaded and detected at runtime, and use classes that are completely unknown during compilation.. We know that when the class is loaded, an object of type Class is generated in the heap memory (a class has only one Class object). This object contains complete class structure information, through which you can know the structure information of the class, such as class methods and properties.

and soThrough a Class object of a class, we can dynamically create instances , call methods , access properties, etc.. When a class is loaded, the JVM automatically generates a Class object. The Class object is the source of reflection. The premise of using the reflection mechanism is to obtain the Class object of the class.
The description of Class is as follows:
Instances of the class Class represent classes and interfaces in a running Javaapplication. An enum is a kind of class and an annotation is a kind of interface. Every array also belongs to a class that is reflected as a Class object that is shared byall arrays with the same element type and number of dimensions. The primitiveJava types (boolean, byte, char, short, int, long, float, and double), and the keyword void are also represented as Class objects.
Class has no public constructor. Instead Class objects are constructed automatically by the Java Virtual Machine as classes are loaded and by calls to the defineClass method in the classloader.

The common operations of the reflection mechanism are:

  • Dynamically load classes and dynamically obtain class information, such as constructors, methods, attributes, etc.;
  • Dynamically construct objects;
  • Dynamically call any methods (public and private), constructors, etc. of classes and objects;
  • To access arbitrary attributes (public and private), when accessing private attributes, you need to set the setAccessible attribute to true. At this time, tell the JVM not to do security checks, you can directly ask, and because no security checks are performed, efficiency is improved;
  • Processing annotations;
  • Get generic information;

The biggest problem with the reflection mechanism is the performance problem. If reflection is used a lot, the performance will be greatly reduced.

2. Bytecode operation

Operating bytecode at runtime is more efficient than reflection overhead, and can achieve the following functions:

  • Dynamically generate new classes;
  • Dynamically change the structure of a certain class, such as adding, deleting, modifying properties or methods, etc.;
  • Common bytecode manipulation libraries are:
    -BCEL
    -ASM
    -CGLIB
    -Javassist

3. Agency

There are two types of agents in Java: static agents and dynamic agents .
Static proxy is to display the source code to create a class proxy. When there are more proxies, you need to write a lot of proxy class source code;

A dynamic proxy is a proxy class that dynamically creates a class. For example, a proxy class can be dynamically generated through JDK, or a proxy class can be dynamically created using bytecode operations. The steps to dynamically create a proxy class through the Proxy and InvocationHandler provided by the JDK are as follows:

  1. Define the interface
  2. Provide implementation class
  3. Implement InvocationHandler
/**
 * 定义接口
 * */

public interface UserDao
{
    
    
   public void add();
}

  /**
    * 实现类
 * */

public class UserDaoImpl  implements UserDao
{
    
    
    public void add()
    {
    
    
    System.out.println("adduser");
    }

}

/**
   * InvocationHandler
 * */
public class ProxyFactory  implements InvocationHandler

{
    
    
     private Object real;
     public ProxyFactory(Object real)
     {
    
    
          this.real=real;
     }   
    /**
     * 通过Proxy来返回代理对象
 * */
     publicObject getProxyObject()
     {
    
    
         returnProxy.newProxyInstance(ClassLoader.getSystemClassLoader(),real.getClass().getInterfaces(),this);
     }

     @Override
     public Object invoke(Object proxy, Method method, Object[] args)
               throws Throwable
     {
    
    
          System.out.println("logging..........");
          Object result=method.invoke(real, args);
          return result;
     }

 

}

Guess you like

Origin blog.csdn.net/m0_51755061/article/details/113099662