The first learning note of Java self scholar (with C language foundation)

Posted by Thora_Fan on Tue, 08 Feb 2022 01:20:18 +0100

Record my first CSDN blog post (it seems to be the first time to write a blog). If there are deficiencies, please correct them.

At present, I am a sophomore in computer science. During this period, the course of software structure has started. To tell the truth, the teachers basically speak from a strategically advantageous position in class. If you really want to master skills, you have to learn by yourself. Then I'll share some experience of learning java for two weeks. Java beginners can make do with it.

First share two links: MIT_6.031,CMU_17-214 , which are the software construction courses of MIT and CMU respectively

Then comes the architecture of Java: Outline of the Collections Framework

I use the IDE of Java. Of course, Eclipse is also OK.

When the basic environment of Java is configured, you can start to really understand the language. First of all, the biggest difference between it and C language is that the main body of C code is a function, while Java is a class. In Java, you can't directly write a main function and output "Hello World!", You must first define a class, such as myClass:

public class myClass {
    public static void main(String[] args) {
        System.out.println("Hello World!");

Note that each Java file can only have one public class with the same name as the Java file name. The main method (equivalent to the main function in C language) is located in this class. For why it should be modified with public static void, please refer to Why should the main method be written as: public static void main(String [] args) {}?

System is Java A class in Lang package is equivalent to a function in C language library. out is a member variable of system. The data type is PrintStream and contains the println method. Its specific function is to output a string and wrap it.

Then we can complete some other tasks. First, if we need to read a file, we can use:

BufferedReader br = new BufferedReader(new FileReader(fileName));

BufferedReader class contains many member methods, including read, readline and close. The read method reads an integer data, the readline method reads an entire line, and the close method closes the file. The specific implementation is as follows:

String myLine = br.readLine();

For string processing, for example, if we want to split the string with tabs, we can use the split method:

String[] a = myLine.split("\t");

The return value of this method is a string array. Note that the habit in Java is String[] a, not like string a [] in C + +.

Definition of array (taking integer as an example):

//m. row and col have been defined and assigned
int[] a = new int[n]; //One dimensional array
int[][] b = new int[row][col]; //Two dimensional array

There is also a concept of regular expression in Java. If we want to represent a kind of string, we can use an expression to describe it. For example, all nonnegative integers can be expressed as [0-9] +. Note that + here does not mean positive, but is equivalent to superscript + in automata theory, which means one or more. The following sentences can be used to judge:

//A is a String type variable that has been defined and assigned
if (a.matches("[0-9]+")) System.out.println("Match!");

matches is a member method of the String class, and the return value is boolean (equivalent to bool in C + +).

The Java language has nouns such as exception and throw, which can be understood as the handling of illegal user input. For example:

try {
    //Try to read in a file
} catch (IOException e) {
    System.out.printf("Failure to open %s!\n", fileName);

In the Java language, if you need a dynamic array, you can select the List class. There are two specific implementations (taking integer as an example):

List<Integer> list1 = new ArrayList<>();
List<Integer> list2 = new LinkedList<>();

The advantages and disadvantages of the two implementations can be understood literally. Note that the angle brackets following the List must be Integer, not int. Similarly, we must use Boolean, Float, Double. For its member methods, it is easy to infer their functions from the method name. The Comparator class is commonly used for sort method parameters, for example:

list.sort(Comparator.comparingInt(Integer::intValue).reversed()); //Reverse order

Specific implementation of set, stack and queue:

Set<Integer> set = new HashSet<>();
Stack<Integer> stack = new Stack<>();
Queue<Integer> queue = new ArrayDeque<>();

If you want to sort an array a, you can use the following statement. The comparator can write it yourself if necessary, but mostly using the comparator class is enough

Arrays.sort(a, fromIndex, toIndex, comparator);

Here, suppose a is an integer array, in which each element is a concrete implementation of a class, which contains the member variable M. Now we need to sort a according to the return value obtained by passing m of each element as a parameter to method f. you can use the following statement:

Arrays.sort(a, Comparator.comparingInteger(o -> f(o.m)));

In addition to some basic methods of Java, testing is also a very important part. Test files are generally placed in the test directory. In IDEA, you can modify the directory type in Project Structure

Add @ Test before the Test method in the Test class. Assertion statements are generally used in Test methods:

myClass A = new myClass([parameters]), B = new myClass([parameters]);

public void functionTest() {
    assertEqual(A.member1, B.member1);

Don't forget to check whether the assertion is feasible before testing

@Test(expected = AssertionError.class)
public void testAssertionsEnabled() {
    assert false;

Thank you very much for seeing here. You are welcome to comment on my questions in the comment area, whether technical or expressive, which is of great help to me.

Topics: Java