Let’s learn Java together!

Pre –

Environment configuration

If you want to do a good job, you must sharpen your tools and honestly configure your environment. For environment configuration, see www.runoob.com/java/java-e…

Then there’s the ritual — HelloWorld!

Create a new project, call it HelloWorld, and add a Java class as follows:

public class helloworld { public static void main(String[] args) { System.out.println("Hello World"); }}Copy the code

The results are as follows:

Using the new Java Ide project: www.evget.com/article/202…

How Java works:

Source code – compiler – output bytecode -Java virtual machine;

Basic knowledge of

Basic grammar

Direct access to the website:

www.liaoxuefeng.com/wiki/125259…

What are strongly/weakly typed languages?

Strongly typed languages are those that enforce type definition, that is, once a variable is typed, it remains that type unless cast. Weak types can be converted automatically as the environment changes;

What is a dynamic/static language?

Dynamic languages, such as Python and Ruby, do data type checking at runtime, that is, they do not assign data types to any variables.

Static languages need to declare data types and check them at compile time;

Java’s package mechanism

Putting functionally similar classes or interfaces in the same package;

The storage mode of tree structure is adopted.

object-oriented

Class, similar to C++;

A virtual class, modified by abstract, cannot create an instance;

Interface, interface modification, only declare method, by its inheritance class method definition, also cannot create instances, implement interface use the implement keyword;

Note: When to use inheritance? When do you use interfaces?

A triangle is a polygon, an apple is a fruit.

A dog has a mouth; a house has a door.

Don’t want some classes to be initialized (that is, create objects)?

For example, base classes, “animals” cannot be initialized, and should not be, because all attributes of animals are not specified! How many legs? A few mouths? In this case, you can use abstract as a modifier to let the compiler know that the class cannot create an instance.

Core classes

Object

Attention! ⚠ ️!

All classes in Java inherit from the Object class, which is the basis for implementing general-purpose classes such as ArrayList.

Aye? In other words, is the Object class (let’s call it the ultimate class, and its objects the ultimate objects) anything more than a general-purpose class? For example — what is stored inside the ultimate object?

— According to the frequency of use and other reasons, the ultimate objects mainly include:

  • Equals () : Determines whether two objects are equal;
  • GetClass () : Gets the current object’s class;
  • HashCode () : the hashCode of the object, a globally unique identifier;
  • ToString () : Gets the name and identity of the class (tentative);

In addition to these general methods, thread synchronization is also served (see below).

Is the Object class abstract?

If it were an abstract class, then all classes would have to implement methods that override them, but in practice, they don’t.

StringBuilder

If you directly use “+” to concatenate strings, each time you concatenate strings, the old object will be thrown away and a new object will be created, which is inefficient.

To address this problem, the Java design class Stringbuilder class is used to efficiently concatenate strings;

StringJoiner

This is used to specify delimiters by string arrays;

The garbage collection

In Java architecture, memory is divided into stacks. Heap, mainly the living space of objects; Stack, which is basically the living space for method calls and variables. So called garbage collection, it’s all heap based.

For a local variable that refers to an object, the local variable itself is on the stack and the reference object is on the heap.

The general object creation process has three steps:

Declare, create, and assign as follows:

Duck duck = new Duck();

Duck Duck is a statement.

New ** creates;

= is an assignment;

When does the object die?

As long as there is a living reference, the object is not dead!

The garbage collector (GC) checks for living references and corresponding objects, and if references to an object are dead, then the object is… dead!

annotations

What are annotations? Special “comments” placed before classes, methods, fields, and arguments in Java source code (comments are visual and are simply ignored by the compiler; Comments are intended for application viewing and are packaged by the compiler into a class file.

Its main function is to configure parameters. Here are some common comments (omitting @) :

  • Configuration: indicates that the current class is a configuration class that is the source of the method bean.
  • Resource: Typically used in tree or set methods to assign values to object parameters;
  • Bean: Used to tell methods to generate a Bean object, which is then handed over to Spring to manage;
  • Component: Hands this class over to Spring to manage;
  • Override: indicates that the subclass overrides the method of the parent class, improving readability and checking by the compiler.

PS: Some Java annotations are used instead of XML configuration;

There are also annotations from Lombok (a Java utility class) :

  • NonNull: Used to check that the object is not empty;
  • The Data;
  • Log

Other details: blog.csdn.net/sunsfan/art…

My.oschina.net/u/2935389/b…

multithreading

Threads are created in Java through the Thread library. There are three main methods: one is to inherit Thread to cover the run () method; Implement Runable interface; Third, lambda syntax is used;

It is worth noting that there is no way to force a thread to interrupt in the new Version of Java, but it can be interrupted by calling interrupt () on the target thread, sending an interrupt request and executing the appropriate logic inside the target thread.

MVC

MVC is not versioning a database. It’s model-view-controller. Model is used to implement complex business logic, View through rendering to achieve the display of the View.

Serialize

Java provides a mechanism for object serialization: an object can be represented as a sequence of bytes (including the object’s data, type information about the object, and so on).

The above byte sequence can be deserialized to create a new object in memory.

How can a class support such a mechanism?

— This class implements the Java.io. Serializable interface, and all attributes of the class must be serializable.

Tool framework

JFrame&Swing

Java’s GUI controls, Swing’s support for components and functionality are more powerful.

Three major Layout Managers:

BorderLayout: Partitions

FlowLayout: From left to right, top to bottom

BoxLayout: Top to bottom

Maven

Maven is A Java project management and build tool that provides A standardized process for building projects and handles dependency management automatically (e.g., A depends on B, B depends on C, A will automatically depend on C). A default Maven project structure is as follows:

There are four kinds of dependencies, which are defined in scope: compile means required at compile time; Test: used when testing. Runtime: not required at compile time, but required at runtime. Provided means required at compile time, but provided by the JDK or server at run time.

PS: So how does Maven know where to download dependencies? — Another central warehouse, where all three libraries go to inquire and download;

Maven’s Lifecycle consists of a series of phases. Take the built-in Lifecycle default for example:

Run MVN + XXX, that is, default to the corresponding phase, such as MVN packge, which will be executed until the package phase.

The common command is claen compile test package.

Executing the phase triggers the goal. What is the goal? Nothing special, is the specific operation, see below:

In fact:

Lifecycle is equivalent to the javaSE package and is a large whole containing many phases;

Phase is equivalent to the JavaSE class, which is part of the whole and contains many goals.

Goal is equivalent to JavaSE’s Method, which is the actual action for each part;

PS: Build a project that will lifecycle until the specified phase.

For large projects, modular management is required, and each small project is a small Maven project, such as a three-module project:

If the same configuration file exists in THE POM. XML file of ABC, you can create the configuration file as an XML file and import it to ABC.

JSP

Servlets are network connection libraries in the Java standard library. It can process HTTP requests and send HTTP responses. PrintWriter can be used to send the response and output HTML, but output is too cumbersome. JSP is used to simplify this operation. A simple JSP (Java Server Pages) is as follows:

<html> <head> <title>Hello World - JSP</title> </head> <body> <%-- JSP Comment --%> <h1>Hello World! </h1> <p> <% out.println("Your IP address is "); %> <span style="color:red"> <%= request.getRemoteAddr() %> </span> </p> </body> </html>Copy the code

The content of the entire JSP is actually an HTML, but with a slight difference:

  • Contained between **<%– and –%>** are JSP comments, which are completely ignored;
  • Contained between **<% and %>** is Java code, which can be written in any Java code;
  • If you use **<%= XXX %>**, you can quickly print the value of a variable.

JSP can simply be thought of as a hybrid of HTML and Java.

The framework

What is a framework?

In simple terms, it is the semi-finished product of Java project development (based on which to develop your own project), consisting of a series of classes and interfaces;

What is the framework to understand at this stage?

Spring boot, Pandora boot;

Filter

In a real Web application, there are usually URL mappings and multiple servlets to handle urls. The following are examples:

Among them, Profile, Post and Reply can only be operated by user login, otherwise it will jump to the login page. If the login judgment is put back into these three, the code is too repetitive, so filter can be used for preprocessing, that is, all requests other than “/user/*” will be screened.

PS: This is essentially a filter (regular matching and the like).

JavaBean

When the class definition meets the following specification:

Several private instance fields (that is, the number of members);

Read and write instance fields via the public method;

And the accessors conform to the following naming conventions (they can only be read/write) :

Public void setXyz(Type value)Copy the code

That’s called a JavaBean.

Obviously, the main purpose of this structure is to transfer data.

Spring

Good qualities

The IoC container

This is an inversion of control called Dependency Injection. IoC separates the creation and configuration of components from the use of components (for example, class A needs class B objects, A and B need A Data object, the traditional way is to need A new Data object in class A and B, but IoC can only need one, the realization of object sharing, creation is very convenient). The components created in the IoC container are called JavaBean components.

PS:JavaBean containers are done by reading XML files using the reflection (representing classes through strings) mechanism.

What is a container?

A container is a software environment that provides the necessary support for a particular component to run. For example, Tomcat is a Servlet container that provides a runtime environment for servlets to run. Software such as Docker is also a container that provides the Necessary Linux environment to run a particular Linux process.

So the core of Spring is to provide an IoC container that manages all lightweight JavaBean components and provides low-level services including lifecycle management, configuration, and so on.

The difference between BeanFactory and ApplicationContext is that the BeanFactory is created on demand, the first time the Bean is retrieved, but the BeanFactory creates all the beans at once. Since the latter inherits the Beanfactory interface, it provides more functionality.

PPS: Annotations are another way to configure beans that are much more convenient (for updating extensions) than XML. See: www.liaoxuefeng.com/wiki/125259…

AOP

Aspect Oriented Programming Unlike OOP (object-oriented, which is characterized by encapsulation, inheritance, and polymorphism), AOP decomposes systems into different concerns (facets).

Why did you want to use the faceted approach? In actual project development, there are many duplicate code segments: for example, security check, log, transaction code, etc. In order to avoid duplication of code, you can use Proxy mode (that is, put this part of the code into Proxy), but this method needs to extract the interface and implement Proxy for each method. AOP is designed to reduce the number of repeated operations.

DI

Dependency injection, which does not need to manually call setXX method to set, but through configuration assignment; (Dependent on inversion of control)

The container

Spring is a container that contains and manages the declaration cycle of application objects.

Componentized one-stop shop

Componentization means that objects can be combined using XML or annotations;

One-stop means the ability to integrate various open source frameworks and third-party libraries;

other

Spring Boot

Spring Boot is a spring-based suite that helps us preassemble a series of Spring components to develop Spring-based Java applications with as little code configuration as possible. It can be simply understood as modular Spring.

PS: highly tool-based modularization, it is recommended to learn the first Spring in the development of SpringBoot, otherwise it is easy to appear problems but can not find a solution

Actual combat tutorial

Reference tutorials:

https://juejin.cn/post/6844903779519725575

PS: If you need to create a New Spring Boot, you need to install Spring Assistant and search for it by yourself.

PS: In the Sprigboot project, the program startup entry Application will only scan the Java code under the current file path;

other

JavaSE

Java Standard Edition Java Standard Edition

classloader

It is used to load classes, and is responsible for converting calSS bytecode forms (which can come from a disk file, a class in a JAR, or a remote byte stream) into an in-memory class object. Bytecode is essentially an array of [] bytes, with a particular complex internal format.

PS: A lot of bytecode encryption is implemented by customizing classLoaders, that is, encrypting bytecode and using specific tools to decrypt it.

There are three classLoaders built into the JVM:

  • BootstrapClassloader: responsible for loading JVM runtime core classes, such as java.util.*, is implemented in C code, called the root loader;
  • ExtensionClassLoader: Responsible for loading JVM extension classes. Such as Swing;
  • AppClassLoader: user-oriented loader, such as a path defined in classpath

default

An interface can only declare a method. After java8, you can use default, which indicates that the method in the interface is a common method and the method body can be written. This is to address “the difficulty of adding new methods to an interface for a released version without affecting existing implementations, it’s all about compatibility”.

Some of the problems

Class over interface:

Blog.csdn.net/qq_35835624…

Java TDO

www.cnblogs.com/xt0810/p/36…

Learning design patterns — common design patterns;

The resources

“HeadFirst Java”

Some links are in the article;