Java Basic Knowledge learning summary (1)
Part 1: Java Collection Containers
directory
Computer principle and operating system
Memory, CPU, hard disk?
Basic Linux Commands
What’s the difference between threads and processes?
Iv. Database foundation
What are the differences between relational and non-relational databases? For example
What is a database index for
When do I need to index a field?
When can an index fail?
What is a slow query?
What is a master slave?
5. Java development fundamentals
The MVC framework?
What does js cross-domain request mean?
What is the error of server 500, 502, 503, 504, 505?
What is load balancing?
What are cookies and sessions? What is the capacity limit for cookies? How does Session work?
Is it possible to implement a Session if the client disallows cookies?
How does Session sharing work? How?
The history of Java
Regular expressions?
Reflection technology?
What are the characteristics of the Java language?
What’s the difference between overloading and overwriting?
What is the difference between an interface and an abstract class?
What are the differences between member variables and local variables?
= = and equals?
The equals and hashCode?
Introduction to hashCode ()
Why have hashCode
JVM
What is bytecode? What are the benefits of adopting bytecode?
The JDK and JRE
Computer principle and operating system
Memory, CPU, hard disk?
Memory is one of the important parts of computer. It is the external memory and THE CPU to communicate with the bridge, all the computer program running in the memory. The strength of memory performance affects the level of the computer as a whole. Memory, also known as internal Memory and main Memory, is used to temporarily store the operational data in the CPU, and the data exchanged with external Memory such as hard disk. As soon as the computer is running, the operating system moves the data it needs to compute from memory to the CPU for computation. When the calculation is complete, the CPU sends the result. Memory also determines how fast the computer runs overall. The memory module consists of a memory chip, a circuit board, and a goldfinger. Computer hard disk is the computer’s most important storage device. Hard disks (Hard Disk Drive, abbreviated HDD) consist of one or more disks made of aluminum or glass. The discs are covered with a ferromagnetic material. Most hard disks are fixed disks that are permanently sealed and fixed in the hard disk drive. Early hard disk storage media were replaceable, but today’s typical hard disk is a fixed storage medium that is enclosed within the disk (except for a filter hole to balance the air pressure). With the development, removable hard disks appeared and became more and more popular and varied. Most of the hard disks installed on microcomputers are called “Winchester hard disks” or “warm disks” for short because of the Winchester technology. Central processing unit (CPU), is one of the main equipment of electronic computer, the core accessories in the computer. Its functions are mainly to interpret computer instructions and process data in computer software. CPU is the core component which is responsible for reading instructions, decoding instructions and executing instructions. The central processor mainly includes two parts, that is, the controller, the arithmetic unit, which also includes the high-speed buffer memory and the realization of the data between them, the control bus. The three core components of an electronic computer are the CPU, internal memory, and input/output devices. The main functions of the CPU are to process instructions, perform operations, control time, and process data.
Basic Linux Commands
1.sudo
This SuperUserDo command is one of the most important for new Linux users to use. Every command that requires root permissions requires this sudo command. You can use sudo before every command that requires root permission. $ sudo su
2.ls(list)
With the list command, the terminal displays all the files and folders in the directory you are working on. Let’s say I’m in the /home folder and I want to see the directories and files inside /home.
/home$ ls
The ls in /home returns the following result:
imad lost+found
3.cd
Change directory (CD) is the main command that is always used in the terminal. It is one of the most basic Linux commands. Using this command is simple. Just enter the name of the folder you want to go to from the current directory. If you want to return to the upper level, simply place the double dot (..) As a parameter. Let’s say I’m in /home, and I want to go to usr, which is always in /home. Here’s how I can use the CD command:
/home $ cd usr
/home/usr $
4.mkdir
Changing directories alone is not complete. Sometimes you want to create a new folder or subtext
The folder. You can do this using the mkdir command. Just put your folder name after the mkdir command in the terminal.
$ mkdir folderName
5.cp
Copy and paste is an important task that we need to accomplish in order to organize our files. Using CP will help you copy and paste files from the terminal. First, you determine which file you want to copy, and then enter the destination to paste the file.
$ cp src des
Note: If you are copying files to a directory that requires root permission for any new file, then you will need to use sudo.
6.rm
The rm command can remove your files and even your directory. If the file needs root permission to be removed, use -f. You can also use -r to recursively remove your folders.
$ rm myfile.txt
7.apt-get
This command varies from distribution to distribution. To install, remove, and upgrade any package in the Debian-based Linux distribution, we can use the Advanced Wrapper Tool (APT) package manager. The apt-get command helps you install software that you want to run in Linux. This is a powerful command-line tool that can perform tasks such as installing, upgrading, and even removing software. In other distributions, such as Fedora and Centos, there are different package managers.
Fedora used to have Yum, but now it has DNF.
$ sudo apt-get update
$sudo dnf update
8.grep
You need to find a file, but can’t remember its exact location or path. Grep can help you solve this problem. You can use the grep command to help you find the file based on the given keyword.
$ grep user /etc/passwd
9.cat
As a user, you often need to look at some document or code from a script. Again, one of the basic Linux commands is the cat command. It will show you the text in the file.
$ cat CMakeLists.txt
10.poweroff
Sometimes, you need to shut down directly from the terminal. This command will accomplish the task. Don’t forget to add sudo at the beginning of the command because it requires root permission to execute poweroff.
$ sudo poweroff
What’s the difference between threads and processes?
The fundamental difference between a process and a thread is that a process is the basic unit of resource allocation in the operating system, while a thread is the basic unit of task scheduling and execution in the processor. Other differences include resource overhead, inclusion relationships, memory allocation, impact relationships, execution processes, and so on.
Resource overhead: Each process has an independent code and data space (program context), switching between programs will have a large cost; Threads can be viewed as lightweight processes. The same class of threads share code and data space. Each thread has its own run stack and program counter (PC), and there is little overhead to switch between threads.
Inclusion: If there are multiple threads in a process, the execution process is not a single line,
It’s done by multiple threads; Threads are part of processes, so they are also called lightweight processes or lightweight processes.
Memory allocation: Threads in the same process share the address space and resources of the same process, but the address space and resources between processes are independent of each other.
Impacts: When a process crashes, it does not affect other processes in protected mode, but when a thread crashes, the entire process dies. So multiple processes are more robust than multiple threads.
Execution process: Each independent process has an entry point for the program to run, a sequence of execution and an exit. But the thread cannot execute independently, must depend on the application program, by the application program provides multiple thread execution control, both can execute concurrently.
The fundamental difference between processes and threads is that processes are the basic unit of resource allocation in the operating system, while threads are the basic unit of task scheduling and execution in the processor.
Iv. Database foundation
What are the differences between relational and non-relational databases? For example
Relational database, refers to the use of a relational model to organize the data of the database, its in the form of rows and columns to store data, in order to facilitate user understanding, relational database this series of rows and columns is called a table, a group of tables constitute a database. The user retrieves data from the database through a query, which is an execution code used to qualify certain areas of the database. A relational model can be simply understood as a two-dimensional table model, and a relational database is a data organization composed of two-dimensional tables and their relationships. For example, mysql, Oracle, and SQL Server NoSQL are non-relational databases. With the rise of web2.0 websites on the Internet, the traditional relational database in dealing with web2.0 websites, especially the super large scale and high concurrency of the SNS type of web2.0 pure dynamic website has been inadequate, there have been a lot of difficult to overcome problems, while the non-relational database has been very rapid development due to its own characteristics. NoSQL database was created to solve the challenges of large data sets with multiple data types, especially big data applications. For example: redis
What is a database index for
In a relational database, an index is a single, physical storage structure that sorts the values of one or more columns in a database table. It is a collection of the values of one or more columns in a table and the corresponding list of logical Pointers to the data pages in the table that physically identify those values. The index function is equivalent to the book catalogue, according to the page number in the catalogue to quickly find the required content.
When do I need to index a field? * * * *
The primary and foreign keys of a table must have indexes.
Tables with data exceeding 300 should have indexes.
Tables that are frequently joined with other tables should have indexes on the joined fields.
Columns in Where order by should be indexed;
Indexes should be built on selective fields;
The index should be built on the field. Do not create an index on the field.
When can an index fail?
Select INDEX(a,b,c) from INDEX(a,b,c); select INDEX(a,b,c) from INDEX(a,b,c);
But when the condition is B,c, the index will not be used.
Compound indexes follow the “leftmost prefix” rule. That is, indexes are used only when the first field of a compound index is used in a query condition. Therefore, the order of the index columns in a composite index is critical. An index cannot be used if the search does not begin by the left-most column of the index.
2. Perform operations on index columns. Operations include (+, -, *, /,!). ,, %, like ‘%_’ (% first), or, in, exist, etc.), causing the index to become invalid.
Select * from test where id = 1;
Select * from test where id=10;
Attention!!!!!
Not in, not exists, not equal! < < > > = This is determined according to the actual query data. If the full scan speed is faster than the index speed, the index is not selected.
3. Apply internal functions to the index, in which case a function-based index should be created.
select * from template t where ROUND(t.logicdb_id) = 1
Copy the code
In this case, set ROUND(t.logicdb_id) as the index.
4, the type is incorrect, for example, the field type is Varchar, where the condition is number.
For example, the template_id field is of type Varchar.
Select * from template t where t.template_id = 1
Select * from template t where t. emplate_id = ‘1’ select * from template t where t. emplate_id = ‘1’
5. If MySQL expects a full table scan to be faster than using indexes, do not use indexes
6. The fuzzy query of like starts with % and the index is invalid
If the index column is not NULL, oracle will consider that the index column may have null values and will not be evaluated according to the index
What is a slow query?
In addition to using the EXPLAIN output execution plan, the way to analyze the performance of MySQL statement queries is to have MySQL record statements where the query exceeds a specified time. We refer to SQL statement queries that exceed a specified time as “slow queries”.
What is a master slave?
1. Perform hot backup for data. As the backup database, the primary database can be switched to the secondary database to continue working after the primary database server fails to avoid data loss.
2. Architecture extension. As the service volume increases, the I/O access frequency is too high to be met by a single machine. In this case, multiple libraries are used to store data to reduce the FREQUENCY of disk I/O access and improve the I/O performance of a single machine.
3, read and write separation, so that the database can support greater concurrency. This is especially important in reporting. Some report SQL statements are very slow, causing the table to be locked and affecting the foreground service. If the foreground uses master and the report uses slave, then the report SQL will not cause the foreground lock, ensuring the foreground speed.
5. Java development fundamentals
The MVC framework?
MVC started out in desktop applications, where M is the business model, V is the user interface, and C is the controller. The purpose of using MVC is to separate the implementation code of M and V, so that the same application can use different representations. For example, a batch of statistics can be represented by a bar chart or a pie chart. The purpose of C is to ensure that M and V are synchronized. Once M changes, V should be updated synchronously. [1-2] Model-View-Controller (MVC) is a software design pattern invented by Xerox PARC in the 1980s for the programming language Smalltalk-80, which has been widely used. It was later recommended as a design pattern for Oracle Sun’s Java EE platform and was increasingly popular with developers using ColdFusion and PHP. The Model-View-controller pattern is a useful toolbox that has many benefits, but also some drawbacks.
What does js cross-domain request mean?
Cross-domain access, in simple terms, is when javascript code from site A tries to access site B, both to submit content and to get content. For security reasons, cross-domain access is forbidden by default by major browsers.
What is the error of server 500, 502, 503, 504, 505?
HTTP 500 – Internal server error
HTTP 502 – Gateway error
503 server is currently unavailable (due to overloading or down for maintenance). Usually, this is a temporary condition. (Service unavailable)
504 indicates a timeout, that is, the request made by the client did not reach the gateway
The 505 server does not support the HTTP protocol version used in the request. (HTTP version not supported)
What is load balancing?
Load balancing builds on the existing network structure and provides a cheap, efficient and transparent way to expand the bandwidth of network devices and servers, increase throughput, enhance network data processing capabilities, and improve network flexibility and availability.
Load balancing means that execution is spread across multiple units of operations, such as Web servers, FTP servers, enterprise critical application servers, and other mission-critical servers, so that work tasks are completed together.
What are cookies and sessions? What is the capacity limit for cookies? How does Session work?
A Cookie is a small piece of text data no more than 4KB, consisting of a Name, a Value, and several other optional attributes that control Cookie validity, security, and usage scope.
Session: In computers, especially in network applications, it is called “Session control”.
The Session object stores the properties and configuration information required for a specific user Session. This way, variables stored in the Session object are not lost when the user hops between the Web pages of the application, but persist throughout the user’s Session. When a user requests a Web page from an application, the Web server automatically creates a Session object if the user does not already have a Session. When a session expires or is abandoned, the server terminates the session. One of the most common uses of the Session object is to store user preferences.
Is it possible to implement a Session if the client disallows cookies?
Can be used. Requires special treatment.
How does Session sharing work? How?
Session replication shares between application servers
Session sharing based on cache DB cache
The purpose of session sharing based on Redis cache is to achieve single sign-on
The history of Java
In 1991, James Gosling and others at Sun Began to develop a language called Oak, hoping to control the microprocessor embedded in cable TV switch boxes, PDAs, etc.
Oak language was renamed Java in 1994;
Three technical architectures of Java:
JAVAEE: Java Platform Enterprise Edition, development of Enterprise environment applications, mainly for web application development;
JAVASE: Java Platform Standard Edition, which completes the development of desktop applications, is the foundation of the other two;
JAVAME: Java Platform Micro Edition, which develops applications for consumer electronics and embedded devices such as mobile phones;
Regular expressions?
Benefits: With the advent of regex, complex manipulation of strings becomes much easier.
Features: Code that operates on strings is represented by symbols. As long as the specified symbol is used, the underlying code can be invoked to manipulate the string. The appearance of symbols simplifies the writing of code.
Disadvantages: Although the appearance of symbols simplifies writing, it reduces readability.
It’s more of a string manipulation problem.
Groups: groups are identified by parentheses. Each parenthesis is defined as a group and is automatically numbered, starting with 1. Whenever a group is used, the corresponding number is the content of the group used. Don’t forget to add \\ to the array. (aaa(WWWW (CCC))(EEE)) A couple of open parentheses is a couple of sets.
Common operations:
1. Matches: Actually uses the matches method in the String class.
String reg = “[1, 9] [0-9] {4, 14}”;
boolean b = qq.matches(reg); // Match the string by associating the re with the string.
2. Split: Use the split method in String.
3, substitution: replaceAll() in the String class;
4. Obtain:
1) first compile regular expressions into regular objects. The static compile(regex) method in Pattern is used.
2), obtain the Matcher object from the Pattern object.
Pattern describes regular expressions and can be parsed. The rule manipulation string needs to be repackaged into the Matcher object.
Then use the methods of the Matcher object to manipulate the string.
How do I get the matcher object?
Through the matcher method on the Pattern object. This method can associate regular rules with strings. And returns the matcher object.
3), you can perform various regular operations on strings using the methods in the Matcher object.
Reflection technology?
Dynamically loads a specified class and retrieves all the contents of that class. And the word
The nodecode file is encapsulated as an object, and the contents of the bytecode file are encapsulated as an object, making it easy to manipulate these members. Simply put: reflection techniques can dissect a class.
Benefits of reflection: greatly increases the extensibility of the program.
The basic steps of reflection:
1. Obtain the Class object, which is a bytecode file object with the specified name.
2. Instantiate the object to get the properties, methods, or constructors of the class.
3. Access properties, call methods, call constructors to create objects.
What are the characteristics of the Java language?
Easy to learn;
Object-oriented (encapsulation, inheritance, polymorphism);
Platform independence (The Java virtual machine implements platform independence);
Reliability;
Security;
Multithreading is supported (the C++ language has no built-in multithreading mechanism, so operations must be called
The multithreaded function of the system to multithreaded programming, but the Java language provides multithreaded support);
Network programming is supported and convenient (the Java language itself was created to simplify network programming
Designed so that the Java language not only supports network programming but is convenient);
What’s the difference between overloading and overwriting?
Overloading: Occurs in the same class, the method name must be the same, the parameter type is different, the number
Method return values and access modifiers can be different, in different order, at compile time.
Override: Occurs in parent class, method name, argument list must be the same, return value range
< or equal to the parent class, throw an exception range less than or equal to the parent class, access modifier range greater than or equal to the parent class; If the parent method access modifier is private, a child class cannot override the method.
What is the difference between an interface and an abstract class?
The default method of an interface is public, and all methods cannot have an implementation in the interface (since Java 8 interface methods can have a default implementation), while abstract classes can have non-abstract methods.
There can be no variables other than static and final variables in an interface, as in an abstract class
. A class can implement multiple interfaces, but only one abstract class. The interface itself can extend multiple interfaces using the extends keyword.
The default modifiers for interface methods are public. Abstract methods can have public, protected, and default modifiers (abstract methods are intended to be overridden so they cannot use the private keyword!). . At the design level, an abstraction is an abstraction of a class, which is a template design, while an interface is an abstraction of behavior, which is a specification of behavior.
Note: In JDK8, interfaces can also define static methods, which can be called directly by the interface name
To use. Implementation classes and implementations are not callable. If two interfaces are implemented at the same time, the default methods defined in the interfaces must be overridden or an error will be reported.
What are the differences between member variables and local variables?
Syntactically, member variables belong to classes, while local variables are defined in methods
A variable or method parameter of; Member variables can be modified by public,private,static, etc., while local variables cannot be modified by access control modifiers or static. However, both member variables and local variables can be modified by final.
From the way variables are stored in memory: if a member variable is static,
Then the member variable belongs to the class. If it is not static, the member variable belongs to the instance. While objects live in heap memory, local variables live in stack memory.
In terms of the lifetime of variables in memory: a member variable is a part of an object that follows
Object creation exists, while local variables automatically disappear with method invocation.
Member variables that have no initial value: are automatically assigned to the default value of the type (a
Exceptions: Member variables that are final must also be explicitly assigned), and local variables are not automatically assigned.
= = and equals?
== : Determines whether the addresses of two objects are equal. That is, determine whether two objects are the same (the basic datatype == compares values, and the reference datatype == compares memory addresses).
Equals () : determines whether two objects are equal. But it can be used in two ways:
Case 1: Class does not override the equals() method. Comparing two objects of this class via equals() is equivalent to comparing the two objects via “==”.
Case 2: Class overrides the equals() method. In general, we override the equals() method to compare the contents of two objects for equality; Return true if their contents are equal (that is, the objects are considered equal).
Here’s an example:
public class test1 { public static void main(String[] args) { String a = new String("ab"); B = new String("ab"); String aa = "ab"; String aa = "ab"; String bb = "ab"; If (aa ==bb) // true system.out.println ("aa==bb"); If (a ==b) // false, not the same object system.out.println ("a==b"); if (a.equals(b)) // true System.out.println("aEQb"); If (42 == 42.0) {// true system.out.println ("true"); }}}Copy the code
Description:
The equals method in String is overridden because the equals method of object compares the memory address of the object, while the equals method of String compares the value of the object.
When an object of type String is created, the virtual machine looks in the constant pool for an existing object with the same value as the one being created, and if so, assigns it to the current reference. If not, create a new String in the constant pool.
The equals and hashCode?
Introduction to hashCode ()
The function of hashCode() is to get the hashCode, also known as a hashCode; It actually returns an int. The function of this hash code is to determine the index position of the object in the hash table. HashCode () is defined in the JDK’s object.java, which means that any class in Java packages the hashCode() function.
A hash table stores key-value pairs. The characteristic of a hash table is that it can quickly retrieve the corresponding value based on the key. Hash code is used in this! (Can quickly find the object you need)
Why have hashCode
Let’s start with “How does a HashSet check for duplications” as an example of why
HashCode: When you add an object to a HashSet, the HashSet will first evaluate the object’s Hashcode value to determine where it was added. It will also compare it to the hashcode values of other objects that have already been added, and if there is no matching Hashcode, the HashSet will assume that the object has not been repeated. However, if objects with the same Hashcode value are found, the equals() method is called to check if the objects with the same Hashcode are really the same. If they are the same, the HashSet will not allow them to be added successfully. If not, it will be rehashed to another location.
(From my Java primer, Head First Java, 2nd edition). In this way, we greatly reduce the number of equals times, which in turn greatly improves the execution speed.
The function of hashCode() is to obtain the hashCode, also known as the hashCode; It actually returns an int. The function of this hash code is to determine the index position of the object in the hash table. HashCode () is useful only in hash tables, not in other cases. The function of hashCode() in a hash table is to get the hashCode of an object, which in turn determines its position in the hash table. HashCode () and equals () states that if two objects are equal, the hashCode must also be the same. If two objects are equal, calling equals on both objects returns true. If two objects have the same hashCode value, they don’t have to be equal. The default behavior of hashCode() is to generate unique values for objects on the heap. If hashCode() is not overridden, the two objects of the class will not be equal in any way (even if the two objects point to the same data)
About the JVM JDK and JRE
JVM
The Java Virtual Machine (JVM) is a virtual machine that runs Java bytecode. The JVM has specific implementations for different systems (Windows, Linux, macOS) that aim to use the same bytecode, and they all give the same results.
What is bytecode? What are the benefits of adopting bytecode?
In Java, code understood by the JVM is called bytecode (that is, a file with a.class extension), which is not processor-specific, but virtual machine oriented. Java language solves the problem of low execution efficiency of traditional interpreted languages by bytecode to a certain extent, while retaining the portability of interpreted languages. So Java programs run more efficiently, and because the bytecode is not specific to a particular machine, Java programs can run on computers with many different operating systems without having to be recompiled.
We need to pay special attention to the.class-> machine code step. This is where the JVM class loader first loads the bytecode file and then executes it line by line through the interpreter, which is relatively slow. Also, there are methods and blocks of code that need to be called frequently (known as hot code), so the JIT compiler was introduced later, and the JIT is a run-time compilation. When the JIT compiler completes the first compilation, it saves the machine code corresponding to the bytecode for direct use next time. We know that machine code is definitely more efficient than the Java interpreter. This explains why we often refer to Java as a compile and interpret language.
HotSpot uses Lazy Evaluation. According to the 80-20 rule of thumb, only a small portion of the code (hot code) consumes most of the system’s resources, which is what the JIT needs to compile. The JVM gathers information about how the code is executed each time and makes some optimizations accordingly, so the more times it executes, the faster it gets. JDK 9 introduced a new Compilation mode, AOT(Ahead of Time Compilation), which compilers bytecode directly to machine code, thus avoiding all the overhead of JIT warm-up. The JDK supports layered compilation and AOT collaboration. However, the AOT compiler is certainly not as good as the JIT compiler.
Conclusion:
The Java Virtual Machine (JVM) is a virtual machine that runs Java bytecode. The JVM has specific implementations for different systems (Windows, Linux, macOS) to use the same bytecode,
They all give the same result. Bytecode and JVM implementations for different systems are the key to the Java language’s “compile once, run anywhere” approach.
The JDK and JRE
The JDK is the Java Development Kit, which is a full-featured Java SDK. It has everything a JRE has, plus a compiler (Javac) and tools (such as Javadoc and JDB). It creates and compiles programs.
JRE is the Java runtime environment. It is the collection of everything you need to run a compiled Java program, including the Java Virtual Machine (JVM), Java class libraries, Java commands, and other building blocks. However, it cannot be used to create new programs.
If you only want to run Java programs, you only need to install the JRE. If you need to do some Java programming, you’ll need to install the JDK. However, this is not absolute. Sometimes, even if you don’t plan on doing any Java development on your computer, you still need to install the JDK. For example, if you are deploying a Web application using JSPS, you are technically just running the Java program in the application server. So why do you need a JDK? This is because the application server converts JSPS into Java servlets, and the JDK is required to compile the servlets.
What is the difference between a static method and an instance method?
Static methods can be called externally using either class name.method name or object name.method name. Instance methods only do this. That is, static methods can be called without creating an object.
When static methods access members of the class, they are only allowed to access static members (that is, static member variables and static methods), but not to access instance member variables and instance methods. Instance methods have no such restriction