Knowledge points of the Java SE part (updated from time to time)

1. static

  • 1. All methods modified with the keyword static in the method declaration are class methods or static methods, and methods not modified by static are called instance methods;

  • 2. Instance methods can call instance methods or class methods in the class, and class methods can only call class methods or static methods of the class, but cannot call instance methods (static methods can only call static methods, not non-static methods)

2. Access modifiers (access rights) in java

access modifier this class Same package Subclass other
private × × ×
default × ×
protected ×
public
  • 1. The outer class cannot directly use the member variables and methods of the inner class. You can first create an object of the inner class, and then access its member variables and methods through the instance object of the inner class;

  • 2. If the outer class and the inner class have the same member variables or methods, the inner class accesses its own member variables or methods by default. If you access the member variables of the outer class, you can use this.member variable name or this.method() to access;

  • 3. Static inner class in java

  • (1) Static inner classes cannot directly access external non-static members, but can be accessed by means of new outer class (). members ;

  • (2) If the static member of the outer class has the same name as the member of the inner class, it can be accessed through the method of classname.static member ; if the static member of the outer class has the same name as the member of the inner class, you can directly call the outer class through the member name static members of the class;

  • (3) When creating an object of a static inner class, you do not need an object of the outer class, you can directly create the inner class: object name = new inner class () ;

3. Other knowledge points

  • 1. The difference between String and Char:

  • (1) String is a string type, and Char is a character type;

  • (2) Char should use single quotation marks, String should use double quotation marks;

  • (3) String is a class with object-oriented features that can call methods

  • 2. Switch conditional statement:

- Switch(表达式){
    case 值:代码;
        break;
        ...
    default:代码;
        break;    
}
  • (1) The value of the expression in the parentheses after Switch must be an integer or a character;

  • (2) The value after the case can be a constant value or a constant expression;

  • (3) After the case is matched, the program code of the matching block is executed. If no break is encountered, the content of the next case block will continue to be executed until the break statement or the end of the Switch statement block is encountered;

  • 3, while (judgment condition) {loop operation}, first judge, then execute

  • 4. do {loop operation}while (judgment condition), execute first, then judge, execute at least once

  • 5. When a method does not need to return data, the return type (modifier) ​​must be void;

  • 6. Intrface is commonly decorated with public, and the system will add abstract by default;

  • 7. The attributes in the interface are constants. Even if the public static final modifier is not added when defining, the system will automatically add it;

  • 8. Implement the interface with the implements keyword

  • 9. Polymorphism is reflected in: parent class name = new subclass ();

  • 10. Array definition: class name [] array name, the data of this type is stored in the array;

  • 11. Multiple for loops: the outer loop controls the row, and the inner loop controls the column;

  • 12. The file name of the public class (public modified) must be consistent with the class name (same)

  • 13. The showSaveDialog() and showOpenDialog() methods determine the file dialog type (JFileChooser object)

  • 14. A Java file can only contain one public class

  • 15. How to create an abstract class: abstract class className{ }

  • 16. Enhanced for loop: (generally used to traverse arrays)

java for(循环变量类型 变量名称:要遍历的对象){ 循环语句; }

//遍历Collection对象
//建立一个collection
String[] string = {"hello","how","are","you"};
Collection list = java.util.Arrays.asList(string);//将数组转换为List对象
//开始遍历
for(String str:list){
    System.out.println(str);
}

//遍历数组
int[] a = {1,5,3,7,6,0};
for(int i:a){
    System.out.println(i);
}
  • 17. Automatic boxing and unboxing of basic data types:

  • Automatic conversion between basic type data and corresponding objects;

  • Unboxing process

graph LR
数据类型-->相应的对象  
  • boxing process

graph LR
数据对象-->数据类型
  • 18. When rewriting the methods of the interface, the methods in the interface must be public abstract methods, so when the class inherits the interface and rewrites the interface method, not only must remove the abstract modifier and give the method body, but must also be clearly marked with public out his access rights;

  • 19. Classes modified by abstract cannot create objects and can only inherit;

  • 20. Access rights:

  • When using a class to create an object in another class, if you do not want the object of this class to directly access its own variables (that is, you cannot directly operate its own member variables through the "." operator), you should assign the member variable access permission. set to private;

  • 21. Static methods can be called by class name.method name ;

  • 22. The infinite loop of the for loop:

    for(;;){
        循环体;
    }
  • 23. The difference between an array and a collection:

  • (1) The length of the array is fixed and immutable;

  • (2) The collection can change dynamically at runtime;

  • 24. Collection framework architecture in java:

graph TD
    A[Collection]
    A -->B[List可重复]
    A -->C[Queue可重复]
    A -->D[Set无序不可重复]
    B -->E[ArrayList]
    C -->F[LinkedList有序]
    D -->G[HashSet]
    B -->F
    H[Map]
    H -->I[HashMap]
  • 25. Before the concept of generics was introduced (Java SE 1.5 introduced the concept of generics), objects stored in collections became Objects, and type conversion was required when taking them out;

  • 26. The iterator traverses the collection:

Interator it = List.interator();
while(it.hasNext()){
    
}
  • 27. Thread:

  • (1) A thread is the smallest unit of execution of a program

  • (2) The relationship between threads: interaction, mutual exclusion, synchronization

  • (3) Thread creation: constructor

  • Thread()

  • Thread(String)

  • Thread(Runnable target)

  • Thread(Runnable target,String name)

//创建方式一:
Class ThreadName extends Thread{
    
}

//创建方式二:
class ThreadTest2 implements Runnable{
    
}

//创建方式三:
Thread thread = new Thread();

//创建方式四:
Thread thread = new Thread(String name);

//创建方式五:
Runnable rb = new Runnable();
Thread thread = new Thread(rd);

//创建方式六:
Runnable rb = new Runnable();
Thread thread = new Thread(rd,String name);
  • (4) Method

void start();   //启动线程
static void sleep(long millis); //线程休眠、毫秒
static void sleep(long millis,int nanos);//休眠
void join();    //使其他线程等待当前线程终止
void join(long millis); //同上
void join(long millis,int nanos);   //同上
static void yield();    //当前运行线程释放处理器资源
static Thread currentThread();  //获取当前线程引用,返回当前运行的线程引用

IO stream part

Commonly used IO streams are: character stream, byte stream, buffer stream, serialization stream, RandomAccessFile class, etc.

1. Byte stream

  • FileInputStream/FileOutputStream

  • BufferedInputStream/BufferedOutputStream

2. Character stream

  • InputStreamReader / OutputStreamWriter

  • BufferedReader / BufferedWriter

  • FileReader / FileWriter

  • Among them, BufferedReader / BufferedWriter is also called character buffer stream , which can read one line at a time and write one line at a time; FileReader / FileWriter is inherited from InputStreamReader / OutPutStreamWriter , InputStreamReader / OutputStreamWriter, BufferedReader / BufferedWriter is inherited from Reader /Writer ;

3. Buffered stream (buffered stream under byte stream)

  • BufferedInputStream / BufferedOutputStream

  • Buffered streams are byte streams

4, RandomAccessFile type

  • The RandomAccessFile class literally means random writing and writing, which means that this class has two methods: writing and writing;

5. Object deserialization stream, serialization stream (ObjectOutputStream, ObjectInputStream)

  • Serialization flow and deserialization flow involve the serialization interface (serializable). To realize the serialization and deserialization of an object, the object must inherit the serialization interface (ie implements Serializable)

  • Note : All streams need to perform the operation of closing the stream (ie close() method) after completing the operation, and at the same time refresh the input stream (ie flush() method) operation;

accumulation of knowledge

  • 1. About abstract classes and interfaces:
    • Classes cannot have multiple inheritance but interfaces can;

    • Neither abstract classes nor interfaces can be instantiated;

  • 2. Two-dimensional array:
    • In the definition of a two-dimensional array, the first bracket must have a value, which cannot be empty, but can be 0;

  • 3. About abstract classes:
    • A subclass can only inherit one abstract class, but can implement multiple interfaces;

    • Abstract classes can have constructors, interfaces have no constructors;

    • An abstract class can have ordinary member variables, but an interface cannot have ordinary member variables;

    • Both abstract classes and interfaces can have static member variables, the access rights of static member variables in abstract classes are arbitrary, and interfaces can only be public static final (default);

    • Abstract classes can have no abstract methods, abstract classes can have ordinary methods, and interfaces are all abstract methods;

    • Abstract classes can have static methods, but interfaces cannot have static methods;

  • 4. Important knowledge points about final:
    • The final keyword can be used for member variables, local variables, methods and classes;

    • Final-modified member variables must be initialized at the time of declaration, or initialized in the constructor, otherwise a compilation error will be reported;

    • You cannot assign a final variable again;

    • Local variables must be assigned a value when they are declared;

    • All variables in anonymous classes must be final variables;

    • Final modified methods cannot be overridden;

    • Final modified classes cannot be inherited;

    • Final variables that are not initialized at the time of declaration are called blank final variables, they must be initialized in the constructor, or call this to initialize, otherwise the compiler will report an error

  • 5. When the operation data type is byte, short, int, char, both numbers will be converted to int type, and the result is also int type (when performing +, -, *, /, % operations)
  • 6. Method input parameters:
    • When the input parameter of the method is a basic type, the value is passed, and the modification of the passed value in the method will not affect the variable that is called;

    • When the input parameter of the method is a reference type, the reference address is passed, and the modification of the passed value in the method will affect the variable at the time of the call;

package com.javasm.work3;

import java.util.Arrays;

public class TestMethod {
    public static void main(String[] args) {
        TestMethod method=new TestMethod();
        int b = 1;
        b = method.test1(b);
        System.out.println(b);
        
        int[] arr = {1,2,3};
        method.test2(arr);
        System.out.println(arr[0]);
        System.out.println(arr);
        Arrays.sort(arr);
    }
    
    /**
     * 方法入参是基本数据类型时,传递的是值
     * 方法内对传递的值进行修改时不会影响调用时的变量
     * @param a
     */
    public int test1(int a){
        a=2;
        return a;
    }
    
    /**
     * 方法入参是引用数据类型时,传递的是内存地址引用
     * 方法内对传递的引用进行修改时会影响调用时的变量
     * @param arr1
     */
    public void test2(int[] arr1){
        System.out.println(arr1);
        arr1[0] = 4;
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324580201&siteId=291194637