timerring

11 Multithreading and Locks

January 13, 2024 · 3 min read
Tutorial
Java

11.1 Multithreading #

11.1.1 Implementing Multithreading #

Approach 1: Extend the Thread class.

class Worker extends Thread {
    @Override
    public void run() {
        for (int i = 0; i < 10; i ++ ) {
            System.out.println("Hello! " + this.getName());
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}

public class Main {
    public static void main(String[] args) {
        Worker worker1 = new Worker();
        Worker worker2 = new Worker();
        worker1.setName("thread-1");
        worker2.setName("thread-2");
        worker1.start();
        worker2.start();
    }
}
Read the full text -> A total of 0.67k words


10 Annotations and Reflection

January 12, 2024 · 1 min read
Tutorial
Java

10.1 Annotations #

(1) Annotations, also called metadata, describe packages, methods, fields, constructors, local variables, and other program elements.
(2) Annotations do not affect program logic, but can be processed at compile time or runtime.
(3) In Java SE, annotations serve relatively simple purposes, such as marking obsolete features or suppressing warnings. In Java EE, they play a more important role, such as configuring aspects of an application in place of the verbose code and XML configuration left over from older Java EE versions.

Read the full text -> A total of 0.40k words


09 Exception Handling

January 11, 2024 · 6 min read
Tutorial
Java

1. Differences Between Error and Exception #

An Error is a failure that a program cannot handle, such as OutOfMemoryError or ThreadDeath. When these occur, the Java Virtual Machine (JVM) generally terminates the thread. Such failures are fatal to the program and cannot be caught and handled.

An Exception is an exceptional condition that the program itself can handle. Exceptions fall into two categories: runtime and non-runtime exceptions. Programs should handle these exceptions whenever possible.

Read the full text -> A total of 1.27k words


08 Common Containers

January 10, 2024 · 2 min read
Tutorial
Java

8.1 List #

Interface: java.util.List<>.

Implementations:

  • java.util.ArrayList<>: resizable array
  • java.util.LinkedList<>: doubly linked list

Methods:

  • add(): add an element at the end
  • clear(): clear the list
  • size(): return its length
  • isEmpty(): check whether it is empty
  • get(i): get the ith element
  • set(i, val): set the ith element to val

Loop syntax:

// Standard
for(int i = 0; i < list.size(); i ++) {  
    System.out.println(list.get(i));  
}
// Enhanced
for (Integer integer : list) {  
    System.out.println(integer);  
}
Read the full text -> A total of 0.58k words


05 Strings

January 7, 2024 · 3 min read
Tutorial
Java

1. Characters and Integers — ASCII Codes #

Each commonly used character corresponds to a number in -128 ~ 127, and the two can be converted to each other. Note: currently, negative numbers have no corresponding characters.

import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        char c = 'a';
        System.out.println((int)c);

        int a = 66;
        System.out.println((char)a);
    }
}

Common ASCII values: 'A'- 'Z' are 65 ~ 90, 'a' - 'z' are 97 - 122, and 0 - 9 are 48 - 57.

In Java, character literals must be enclosed in single quotes and string literals in double quotes. They are distinct. (Python and JS do not make this distinction.)

Characters can participate in arithmetic; they are treated as integers during operations:

import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        int a = 'B' - 'A';
        int b = 'A' * 'B';
        char c = 'A' + 2;

        System.out.println(a); // 1
        System.out.println(b);
        System.out.println(c);
    }
}
Read the full text -> A total of 0.60k words


04 Arrays

January 6, 2024 · 4 min read
Tutorial
Java

One-Dimensional Arrays #

Defining Arrays #

Array definitions are similar to variable definitions.

public class Main {
    public static void main(String[] args) {
	    int[] a;// Definition: define an array a
		a = new int[10]; // Initialization: initialize a as an array of length 10, meaning a contains 10 int variables; equivalent to =>int[] a = new int[10]// Define and initialize at the same time
        int[] a = new int[10], b;
        float[] f = new float[33];
        double[] d = new double[123];
        char[] c = new char[21];
        // Arrays can be defined for more than just primitive data types; String arrays can also be defined
        String[] strs = new String[10];// This array contains 10 String variables
    }
}
Read the full text -> A total of 0.92k words


02 Conditional Statements

January 4, 2024 · 2 min read
Tutorial
Java

If Statements #

if-else statements can also contain other if-else statements.

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt(), b = sc.nextInt(), c = sc.nextInt();

        if (a > b) {
            if (a > c)
                System.out.println(a);
            else
                System.out.println(c);
        } else {
            if (b > c)
                System.out.println(b);
            else
                System.out.println(c);
        }
    }
}

Chained if-else statements

Enter a score between 0 and 100.
If it is at least 85, output A;
if it is at least 70 but less than 85, output B;
if it is at least 60 but less than 70, output C;
if it is less than 60, output D.

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int s = sc.nextInt();

        if (s >= 85) {
            System.out.println("A");
        } else if (s >= 70) {
            System.out.println("B");
        } else if (s >= 60) {
            System.out.println("C");
        } else {
            System.out.println("D");
        }
    }
}
Read the full text -> A total of 0.45k words