1, Java features 1, object-oriented
- Despite the influence of its predecessors, Java was not designed to be compatible with the source code of other languages. This allows the Java development team the freedom to start from scratch. One result of this is that the Java language can approach objects more directly, easily, and realistically. Java’s object model is simple and easily extensible, maintaining high performance for simple data types, such as integers, but not objects.
2. Explanatory and high performance
- Bytecode can be interpreted and executed on any system that provides a Java Virtual Machine (JVM). Many of the earlier attempts at cross-platform solutions have been performance demanding. Other language systems that interpret execution, such as BASIC, Tcl, and PERL, have insurmountable performance flaws. Java, however, runs well on very low cpus. As explained earlier, Java is indeed an interpreted language, and Java’s bytecode is carefully designed so that it is easy to convert bytecode directly into high-performance native code using JIT compilation techniques. Java runtime systems provide this feature while still being platform independent, so “efficient and cross-platform” is no longer a contradiction for Java.
3, dynamic
- Java programs come with a variety of runtime type information to validate and resolve object access problems at run time. This makes it possible to connect code dynamically in a safe and efficient manner, and is important for the robustness of a small application environment, since small programs within bytecode can be updated dynamically in a runtime system.
2. Object-oriented programming 2.1 Abstraction
- A substantive element of object-oriented programming is abstraction. People deal with complexity through abstraction.
- Instead of thinking of a car as a set of tens of thousands of independent parts, for example, people think of the car as an object with its own unique behavior. This abstraction makes it easy to drive a car to the grocery store without being overwhelmed by the complexity of the parts that make up the car. The data of traditional procedural programs can be represented by several component objects through abstraction, and the process steps in the program can be regarded as message collection among these objects. In this way, each object has its own unique behavioral characteristics. You can think of these objects as concrete entities and have them react to messages telling them what to do. This is the essence of object-oriented programming. The concept of object orientation is at the heart of Java.
2.2 Three Principles of Object-oriented programming
- All object-oriented programming languages provide mechanisms to help you implement an object-oriented model. These mechanisms are encapsulation, inheritance, and polymorphism. Now let’s look at their concept.
encapsulation
- Encapsulation is a programming mechanism for binding code and the data it processes together to ensure that both programs and data are protected from external interference and misuse. One way to understand encapsulation is to think of it as a black box that prevents externally defined code from arbitrarily accessing internal code and data. Access to the code and data inside the black box is strictly controlled through a properly defined interface.
- The basic unit of Java encapsulation is the class. Although classes will be covered in more detail in a later section. It is still worth discussing briefly. A class is a logical structure, whereas an object is a physical entity that actually exists. For those of you familiar with C/C++, Java programmers call methods what C/C++ programmers call functions. In a program written entirely in Java, methods define how member variables are used. This means that the behavior and interface of a class are defined through methods that operate on its instance data.
inheritance
- Inheritance is the process by which one object acquires the properties of another object. Inheritance is important because it supports the concept of hierarchical classification. With inheritance, an object only needs to define the attributes that make it unique in its own class, because it can inherit all the common attributes from its parent class.
- Inheritance and encapsulation interact. If a given class encapsulates some properties, any of its subclasses will have the same properties and add attributes unique to the subclass. This is a key concept for object-oriented programs that grow linearly rather than geometrically in complexity. The new subclass inherits all attributes of all its ancestors. It does not interact unpredictably with most of the rest of the code in the system.
polymorphism
- Polymorphism is a feature that allows an interface to be used by multiple actions of the same kind, depending on the application. Let’s take a lifO stack as an example. Suppose you have a program that needs three different types of stacks. One stack for integer values, one for floating point values, and one for characters. Although the types of data stored in the stack are different, the algorithm for implementing each stack is the same. In a non-object-oriented language, you create three different stack programs, each with a name. However, with Java, because it is polymorphic, you can create a generic stack assembly that shares the same name. The concept of polymorphism is often described as “one interface, many methods”. This means that you can design a common interface for a set of related actions. Polymorphism allows the same interface to be used by multiple actions of the same class, thus reducing the complexity of the program. It is the task of the compiler to select the specific actions (that is, methods) to apply to each situation; the programmer does not have to do this manually. You just have to remember and use the generic interface.
Third, a hashmap hashtable
- A HashMap is a hash table that stores a key-value mapping. HashMap extends AbstractMap and implements Map, Cloneable, java.io.Serializable interfaces.
- The implementation of HashMap is not synchronous, which means it is not thread-safe. Both key and value can be null. In addition, the mappings in a HashMap are not ordered. Instances of a HashMap have two parameters that affect its performance: “Initial capacity” and “load factor.” Capacity is the number of buckets in the hash table. The initial capacity is just the capacity of the hash table when it was created. The load factor is a measure of how full a hash table can be before its capacity automatically increases. When the number of entries in a hash table exceeds the product of the load factor and the current capacity, the hash table is rehash (that is, rebuild the internal data structure) so that the hash table has approximately twice the number of buckets.
JVM memory model 1
- Each thread has a separate program counter that records the instruction to be run. A thread-private memory area. If a JAVA method is executed, the counter records the JAVA bytecode address being executed, and if a native method is executed, the counter is empty
2. Vm stack
- Thread-private, created at the same time as the thread. Manages an in-memory model for JAVA method execution.
- Local method area
- It functions like a virtual stack, but manages native methods instead of JAVA methods
3. The method of area
- Thread-shared metadata information used to hold classes loaded by the virtual machine: constants, static variables, just-in-time compiler compiled code. Also called permanent generation.
4. The JAVA heap
- Thread shared, holding all object instances and arrays. The main area for garbage collection. It can be divided into the new generation and the old (tenured).
5. Runtime type information (RTTI + reflection)
- Concept – RTTI: Runtime type information enables you to discover and use type information at runtime.
- How to use: There are two main ways in which Java allows us to recognize information about objects and classes at runtime (and a third way, which is helpful, is described below) :
- One is “traditional” RTTI, which assumes that we already know all types at compile time, such as Shape s = (Shape)s1; The other is a “reflection” mechanism that runs information about the classes we discover and use at runtime, using class.forname. There’s actually a third form, which is the keyword instanceof, which returns a bool, which keeps the notion of type, which means “Are you this class? Or are you a derivative of this class?” . And if you compare actual Class objects with == or equals, inheritance is not considered — either it’s the exact type, or it’s not.
The working principle of
- To understand how RTTI works in Java, you must first know how type information is represented at run time. This work is done by special objects called Class objects, which contain class-related information. Java sends Class objects to perform its RTTI, using a subsystem implementation of the Class loader.
- Whenever you want to use type information at runtime, you must first get a reference to the appropriate Class object
The difference between reflection and RTTI
- The only real difference between RTTI and reflection is that, for RTTI, the compiler opens and examines.class files at compile time (that is, all methods of an object that can be called using normal methods); For reflection,.class files are not available at compile time, so they are opened and examined at run time.
Just-in-time compiler technology – JIT
- There are a number of additional technologies In the Java VIRTUAL machine to speed things up, especially those associated with loader operations called just-in-time (JIT) compilers. This technique can speed up a program by translating all or part of it into native machine code (which is the job of the JVM). When a class needs to be loaded, the compiler finds its.class file and loads the bytecode for that class into memory. At this point, there are two options:
- One is to have the just-in-time compiler compile everything.
- Another practice is called lazy evaluation, which means that the just-in-time compiler compiles code only when necessary, so that code that is never executed may never be JIT compiled at all.
7. Final keyword Misunderstanding of final keyword
- When final is applied to a base data type, it means that the value is constant (i.e. a compile-time constant, or a static final). When final is applied to an object reference rather than a base type, it can be a little confusing because when applied to an object reference, Final makes a reference constant; once a reference has been initialized to one object, it cannot be pointed to another. However, objects themselves can be modified, and Java does not provide a way to make any object invariant (though you can write your own classes to do so). The same limitation applies to arrays, which are also objects. These are the basics and some of the key points you need to master when learning Java. Understanding them is important for learning Java