Packaging
Java provides two type systems, basic type and reference type. Using basic types is efficient. However, in many cases, objects will be created for use, because objects can do more functions. If you want our basic types to operate like objects, you can use the wrapper classes corresponding to the basic types, as follows:
Overview of packing and unpacking
The process of back and forth conversion between the basic type and the corresponding packing class object is called "packing" and "unpacking":
- Boxing: convert from basic type to corresponding wrapper class object.
- Unpacking: convert from packing class object to corresponding basic type.
Automatic packing and unpacking
Since we often need to convert between basic types and packaging classes, starting from Java 5 (JDK 1.5), the boxing and unpacking of basic types and packaging classes can be completed automatically. For example:
public class Test { public static void main(String[] args) { /* Packing and unpacking: - Boxing: convert from basic type to corresponding wrapper class object. - Unpacking: convert from packing class object to corresponding basic type. Automatic packing and unpacking: - Auto packing: automatically convert from basic type to corresponding packing class object. - Automatic unpacking: automatically convert from packaging object to corresponding basic type. */ // Packing // int--->Integer Integer i1 = new Integer(10);// The integer represented by the i1 object is 10 Integer i2 = Integer.valueOf(10);// The integer represented by the i2 object is 10 // Unpacking // Integer--->int int num1 = i1.intValue(); int num2 = i2.intValue(); System.out.println("=========================="); // Automatic packing Integer i3 = 10; // Automatic unpacking int num3 = i3; } }
Convert basic type to String
- Method 1: add an empty string directly after the number
- Method 2: use the String class static method valueOf()
//int --- String int number = 100; //Mode 1 String s1 = number + ""; System.out.println(s1); //Mode 2 //public static String valueOf(int i) String s2 = String.valueOf(number); System.out.println(s2); System.out.println("--------");
Convert String to base type
Except for the Character class, all other wrapper classes have parseXxx static methods that can convert string parameters to corresponding basic types:
// Basic type -- > string: String str1 = 100 + "";// Contents of str1 string: "100" // String -- > basic type: int num1 = Integer.parseInt(str1); System.out.println(num1+num2);// 200
generic paradigm
What is generics? Generics: when defined, it represents an unknown data type, and its specific data type is determined when it is used. When a collection does not use generics, any type can be saved when it is saved. However, it is difficult to operate when it is taken out. Using generics directly controls the type at compile time, and can only store the data defined by generics. The role of generics Is to determine the specific type of an unknown type when creating an Object. When no generic type is specified, the default type is Object type.
Define and use classes that contain generics
Use generics: generics are not specific types when defined, but specific types when used. Determine the specific data types of generics when used.
Code example, defining generic classes
// Meaning generic class public class MyArrayList<E> { E e; public E method(E e){ return e; } }
Use classes with generics
class Test { public static void main(String[] args) { /* Use a class with generics: when creating this class object, determine the specific data type of this class generics When to define generic classes: When the formal parameter type \ return value type of a member variable or member method in a class is uncertain, You can define this class as a class that contains generics */ //The generic type is String MyArrayList<String> list1 = new MyArrayList<>(); // The generic type is Integer MyArrayList<Integer> list2 = new MyArrayList<>(); } }
Define and use methods that contain generics
for example
public class Test { // Define methods with generics public static <T> T method1(T t){ return t; } }
Use generics: determines the type of generics when a method is called
class Test { public static void main(String[] args) { /* Define methods with generics: Modifier < generic variable > return value type method name (formal parameter list){ Method body } Generic variable: any letter will generally write T,M Use methods with generics: when calling methods with generics, determine the specific data type of their generics When will methods with generics be defined: If the parameter type or return value type of a method in a class is uncertain, the method can be defined as a method with generic type */ Integer i1 = method1(100);// The specific data type of the specified generic is Integer System.out.println(i1);// 100 System.out.println("============================"); String s = method1("it");// The specific data type of the specified generic is String System.out.println(s);// it } // Define methods with generics public static <T> T method1(T t){ return t; } }
Define interfaces with generics
public interface IA<E> { public abstract void method1(E e); public default E method2(E e){ return e; } }
Using generics method 1: determine the type of generics when defining and implementing classes
// The specific data types of interface generics are determined by implementing classes public class Imp1 implements IA<String> { @Override public void method1(String s) { } @Override public String method2(String s) { return null; } }
At this point, the value of generic E is of type String.
Use generic method 2: you never determine the type of generic type until you create an object
// When implementing the interface, the class does not determine the specific data type of the interface generic, // Instead, the specific data type of the interface generic is determined when creating the implementation class object public class Imp2<E> implements IA<E> { @Override public void method1(E e) { System.out.println("Implementation class method1"); } @Override public E method2(E e) { return e; } }
Generics are unknown data types. Generics defined on classes will determine the type of generics when using classes, generics defined on methods, generics defined on interfaces, and generics when using interfaces.
Generic wildcard
Generic wildcard: when you don't know what type to use to receive, you can use?,? Indicates unknown wildcard. At this time, you can only accept data and can't store data in the collection.
public static void method2(ArrayList<?> list){ //Other codes }
Restricted generics
In fact, you can set generics arbitrarily as long as they are classes, but you can specify the upper and lower limits of generics in JAVA generics.
Upper limit of generics:
- Format: type name <? Extensions class > object name
- Meaning: only this type and its subclasses can be accepted
Lower bound of generics:
- Format: type name <? Super class > object name
- Meaning: only this type and its parent type can be accepted
For example, we now know Object class, String class, Number class and Integer class, where Number is the parent class of Integer.
import java.util.ArrayList; public class Test { public static void main(String[] args) { /* Advanced use of wildcards -- restricted generics: Upper limit: <? Extensions class name > can only receive this class type or its subclass type Lower limit: <? Super class name > can only receive this class type or its parent class type */ // Relationship: String inherits Object,Integer inherits number, and number inherits Objec ArrayList<Object> list1 = new ArrayList<>(); ArrayList<String> list2 = new ArrayList<>(); ArrayList<Integer> list3 = new ArrayList<>(); ArrayList<Number> list4 = new ArrayList<>(); } // Define a method that can only receive the above list3 and list4 sets public static void method2(ArrayList<? extends Number> list){ } // Define a method that can only receive the above list3, list4 and LIST1 sets public static void method3(ArrayList<? super Integer> list){ } // Define a method that can receive the above four sets public static void method1(ArrayList<?> list){ } // Define a method that can receive the above four sets public static void method(ArrayList list){ } }
Common data structures
The common structures of data storage are stack, queue, array, linked list and red black tree.
Stack
-
Stack: stack, also known as stack, is a linear table with limited operation. Its limitation is that insertion and deletion operations are only allowed at one end of the table, and addition, search, deletion and other operations are not allowed at any other location.
To put it simply: the set with this structure has the following characteristics for accessing elements
- First in and then out (that is, the stored elements can only be taken out after the elements behind them are taken out in turn). For example, when the bullet is pressed into the cartridge clip, the first pressed bullet is below, and the second pressed bullet is above. When shooting, the bullet above can be ejected first, and then the bullet below can be ejected.
- The entrance and exit of the stack are at the top of the stack.
Here are two nouns to note:
- Stack pressing: is to store elements. That is, the elements are stored at the top of the stack, and the existing elements in the stack move one position to the bottom of the stack in turn.
- Spring stack: that is, take elements. That is, take out the top position elements of the stack, and the existing elements in the stack move one position to the top of the stack in turn
queue
- Queue: queue, abbreviated as queue, is also a linear table with limited operation like stack. Its limitation is that it is only allowed to insert at one end of the table, and take out and delete at the other end of the table.
In short, the set with this structure has the following characteristics for accessing elements:
- First in, first out (that is, the stored element can be taken out only after the elements in front of it are taken out in turn). For example, when a small train passes through a cave, the front goes first and the rear goes in; The front comes out first and the rear comes out later.
- The entrance and exit of the queue occupy one side respectively. For example, in the following figure, the left side is the entrance and the right side is the exit.
array
-
Array: array is an ordered sequence of elements. Array opens up a continuous space in memory and stores elements in this space. Like a row of rental houses, there are 100 rooms, from 001 to 100, and each room has a fixed number. Through the number, you can quickly find the renter.
In short, the set with this structure has the following characteristics for accessing elements:
- Find elements: through the index, you can quickly access the elements at the specified location
- Adding and deleting elements is slow : You need to create a new array to add and delete elements
Linked list
-
Linked list: linked list is composed of a series of nodes (each element in the linked list is called a node), which can be generated dynamically at run time. Each node consists of two parts: one is the data field that stores the data element, and the other is the pointer field that stores the address of the next node. We often say that the linked list structure has one-way linked list and two-way linked list.
In short, the set with this structure has the following characteristics for accessing elements:
- Multiple nodes are connected by address. For example, many people hold hands. Each person uses his right hand to hold the next person's left hand, and so on, so that many people are connected together.
- Slow to find elements: to find an element, you need to find the specified elements backward through the connected nodes.
- Fast addition and deletion of elements: you only need to modify the address value of the next element linked
Red black tree
Red black tree is a self balanced binary search tree, which is a data structure used in computer science. The role of red black tree: improving search efficiency