Quick final review of object-oriented Java

The exam will be held tomorrow. Today, there is a wave of review and continuous updating...

JAVA core knowledge points induction

Fundamentals of programming

1, Basic knowledge

  1. identifier

    Java identifiers are the names given to variables, classes, or methods. A simple understanding is to name yourself.

    Rule: 1 Letters, underscores () Beginning with USD ($)

    ​ 2. Letters, underscores () Starting with dollar ($) and consisting of numbers

    ​ 3. Case sensitive, no length limit, no keywords

  2. data type

    • Basic (simple) data type

      Numeric type: integer type (byte, short, int, long), floating point type (float, double)

      Character type: '' (Java adopts Unicode encoding international standard)

      Boolean: true, false

      Data type conversion: (int), math round(),Integer.parseInt()

    • Composite (Reference) data type

      Class, interface, array

  3. Variables and Assignment

    Pay special attention to scope

  4. Operators and Expressions

    • Basic assignment operator=

    • Arithmetic operators + - */

    • Relational operations >, > =, <, < =, ==

      The result of the operation is a Boolean value

    • Boolean operations &, |!

      &&(short circuit and) the left is false, the whole is false, and the right is not operated

      ||(short circuit or) the left side is true, the whole is true, and the right side will not operate

      The result of the operation is a Boolean value

    • Bit operations (integer type) > >, <, > >, &, |, ^~

    • Conditional operation exp? value1 : value2

      (is exp true? If it is true, execute value1 and if it is false, execute value2)

    • Compound assignment operation + =, - =, * =, =,% =, < < =&=

  5. character string

    Different from characters, string uses' ', character uses''

  6. if statement

    if else,if elseif elseif else

  7. switch Statements

    case: case: break;default;

  8. for loop

    for (initialization expression); loop condition expression (boolean type); Post loop operation (expression)

  9. Enhanced for loop

    Access the whole array + read the elements in the array (without modification) + no subscript is required for processing

    Format: for (parameter 1: parameter 2) {execute statement}

    Parameter 1: define a variable to store the elements in the array (for example, int i)

    Parameter 2: array name to be traversed

    If you want to output each data element in the execution statement, you can directly sout h (i + "")

  10. while,dowhile

  11. break: jump out of the whole cycle

  12. continue: jump out of the current cycle

2, Method

  • Definition method

    Specify two points: 1. The parameter type passed in; 2. The data type returned by the method

  • Call method

  • Recursive Method

  • Method overloading: the simple understanding is that the method name is the same, but the type or number of parameters passed in is different

  • Method Rewriting: the method name is the same, but the method body is different

3, Array

  • Array declaration:

Static initialization: enumerations

Dynamic initialization: declare reference space + allocate space + assign initial value

​ int[] a = new int[10];

  • Multidimensional array

    Static: int[][] [][] [] [] a={(1,2),(2,3),(3,4)}

    Dynamic:

    int[ ] [ ] a=new int[2] [3];

    a[0] [0] =1;

object-oriented

1, Classes and objects

UML: class name + attribute + method (* * + public, - private, # * * protected)

  1. Access modifier:

    public: when a member is declared, all other classes can access it

    private: when decorating a member, it can only be accessed by the class

    protected: the current package and its inherited subclasses (both inside and outside the package)

    Default: it can be accessed in the same package

  2. object

    Member variable: can be accessed within the class

    Local variable: within a code block or a specific method

    (it is usually necessary to use the content of a local variable and find that it is local. At this time, the scope of action should be enlarged outside the method so that it can be obtained.)

  3. Construction method (reloadable)

    A simple understanding of overloading and Rewriting: overloading is different from the parameters passed in by the method, and rewriting is different from the content of the method (i.e. method body).

    There are three methods: with parameters (construct and pass in what member variables are needed), without parameters, and default (without parameters)!!

    **!!!** Note: if a construction method with parameters is defined, the system will not generate a construction method without parameters by default,

    If necessary, you need to define a parameterless

  4. this keyword

    It is mainly used in the construction method, using * * this** It refers to the member variable defined at the top of the class. Pass the data passed in the constructor to the member variable

  5. static modifier

The simple understanding is that with the static modification, the system automatically initializes the allocated space and can be used directly without instantiation.

Advanced object oriented

1, GUI

2, Exception handling

3, Collection class

ArrayList

The add() method passes in the address of the reference

TreeSet

​ add()

HashMap

put ("key", "value")

Iterator iterator
Iterator<object> it = object.iterator();
		while(it.hasNext()) {
			object type temp = it.next();
			System.out.println(temp.print());
		}

4, Input / output stream

5, Multithreading

6, Network programming

InetAddress.getLocalHost(),URL,Socket

TCP

The principle is that new is a server that has been listening, and then reads information through the input stream and sends information through the output stream. For multi person chat, the run method of sending and receiving information is encapsulated by multi threads.

The client directly connects to the server with the same IP + port number. It also sends and receives information through input and output streams.

After this part of the content is understood in meditation, directly sink into silence for three times!

Server side

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;

public class multiServer {
    //Define a collection class to store clients
    static ArrayList<Socket> array = new ArrayList<>();
    
    public static void main(String[] args) {
        System.out.println("==============Waiting for connection===========");
        try {
            ServerSocket ss = new ServerSocket(9001);
            while (true) {
                //Keep the server listening and allow the client to connect,
                Socket socket = ss.accept();
                System.out.println( socket.getInetAddress()+ "Connected");
                //add once for each connection
                array.add(socket);
                //Create a server-side thread for the currently connected socket to receive and send messages
                receive r1 = new receive(socket);
                r1.start();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    static class receive extends Thread {
        Socket socket;
        public receive(Socket socket) {
            this.socket = socket;
        }
        @Override
        public void run() {
            super.run();
            try {
                while (true) { //Continuously receive information from the client
                    InputStream inputStream = socket.getInputStream();//Use the input stream obtained from socket as the creation input stream
                    //byte is used to store the data obtained from the client
                    byte[] bytes = new byte[1024];
                    inputStream.read(bytes);
                    String s = new String(bytes);//Put the data in the array into a string
                    //Traverse each Socket object in the collection, create output streams respectively, and send the information sent by the client
                    for (int i = 0; i < array.size(); i++) {
                        OutputStream outputStream = array.get(i).getOutputStream();
                        outputStream.write(s.getBytes());
                        outputStream.flush();
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

Client

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Scanner;


public class multiClient {
    public static void main(String[] args) {
        try {
            Socket soc = new Socket("192.168.3.8", 9001);
            System.out.println("Please enter the information to send:");
            Scanner sc = new Scanner(System.in);
            String str = sc.nextLine();

            OutputStream outputStream = soc.getOutputStream();
            outputStream.write(str.getBytes());
            outputStream.flush();

            while (true) {
                InputStream inputStream = soc.getInputStream();
                byte[] bytes = new byte[1024];
                inputStream.read(bytes);
                String s = new String(bytes);
                System.out.println("Received message from server:" + s);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

UDP

Using datagram socket and datagram packet

Send and receive information through the send() and receive() methods

Keywords: Java Back-end

Added by andyjimmy on Sun, 09 Jan 2022 15:28:13 +0200