[learn Java notes from scratch] common API s

Posted by maltech on Tue, 07 Apr 2020 18:19:16 +0200

You can pay attention to the author's account and the Java notebook from scratch. You can also go to the author's blog Garden to learn from the catalog. This film will be based on the black horse programmer job class video for learning and data sharing, and take notes and their own views. Welcome to study and discuss together.

[learn Java notes from scratch] directory

Java itself provides us with a lot of encapsulated APIs, which can be called directly in development, greatly improving the efficiency of development. In this section, we will teach you how to consult and use the API, as well as some common APIs

Links: https://pan.baidu.com/s/1fwlb0ilG7DEJwSnfCjSurg
Extraction code: kt54
This is the API document

Class Scanner

Take the Scanner class as an example, the fishing method of Jiaotong University
Step 1: open the API document after downloading

Step 2: at the index, enter the class or method you want to learn

Step 3: click the Scanner class to open a dialog box. Click the first one

Step 4: enter to view what you need

1: look at the bag.
The classes under the java.lang package do not need to import packages when they are used. You can see that the Scanner class is under the java.util package, so you need to import packages when you use it
import java.util.Scanner;
2: Look at the description of the class
A simple text scanner that can use regular expressions to parse basic types and strings.
The following code enables the user to read a number from System.in:

 Scanner sc = new Scanner(System.in);
 int i = sc.nextInt();

3: On the construction method
Scanner(InputStream source): construct a new scanner, whose generated value is scanned from the specified input stream.
Scanner sc = new Scanner(System.in);
4: See member method
String nextLine(): this scanner executes the current line and returns skipped input information.
String s = sc.nextLine();
Call method:
Look at the type of return value: you can receive the type returned by others
Look at the method name: don't write the name wrong
Look at the formal parameters: if someone wants several parameters, you can give them to them. If they want any data type, you can give them to them
5: In the later stage, we also need to know whether the child parent class, interface, and method of this class are abstract, static, etc


import java.util.Scanner;

public class Test {
	public static void main(String[] args) {
		System.out.println("Please input:");
		Scanner sc = new Scanner(System.in);
		String s = sc.nextLine();
//Output result
//Please input:

String string class

A string of data consisting of multiple characters, the essence of which is an array of characters

Construction method

String(String original): encapsulates string data into string objects
String(char[] value): encapsulate the data of character array into string object
String(char[] value, int index, int count): encapsulate part of the data in the character array into a string object

public class StringDemo {
	public static void main(String[] args) {
		// Mode 1
		// String(String original): encapsulate string data into string objects
		String s1 = new String("hello");
		System.out.println("s1: " + s1);
		// Mode 2
		// String(char[] value): encapsulate the data of character array into string object
		char[] chs = { 'h', 'e', 'l', 'l', 'o' };
		String s2 = new String(chs);
		System.out.println("s2:" + s2);
		// Mode 3
		// String(char[] value, int index, int. count): encapsulate part of the data in the character array into a string object
		String s3 = new String(chs, 0, chs.length);
		String s4 = new String(chs, 1, 3);
		System.out.println("s3:" + s3);
		System.out.println("s4:" + s4);

//Output result
s1: hello

Member method

It's very simple. You can try it yourself.

First, a concept Object is introduced: it is the root class in the class hierarchy, and all classes inherit from it directly or indirectly.
If the formal parameter of a method is 0object, then we can pass any subclass object of it here.

Judgment function of String class

boolean equals(Object obj): compare whether the contents of strings are the same
boolean equalsIgnoreCase(String str): compare whether the contents of strings are the same, ignoring case
boolean startsWith(String str): determines whether the string object starts with the specified str
boolean endsWith(String str): determines whether the string object ends with the specified str

Getting function of String class

int length(): get the length of the string, which is actually the number of characters
char charAt(int index): gets the character at the specified index
int indexOf(String str): gets the index of the first occurrence of str in a string object
String substring(int start): intercept string from start
String substring(int start, int end): intercept string from start to end

Conversion function of String class

char[] toCharArray(): convert string to character array
String towercase(): converts a string to a lowercase string
String toUpperCase(): convert string to uppercase

Other functions of String class

String trim(): remove spaces at both ends of string
String[] split(String str): split the string according to the specified symbol

StringBuilder class

If we do String splicing, every time we splice, we will build a new String object, which is time-consuming and waste space. StringBuilder can solve this problem. But in fact, StringBuilder class is not commonly used. In the future, we will introduce a more convenient and object-oriented collection class AarryList class, so you can learn about StringBuilder class.

Class object

String toString(): returns the string representation of the pair of images

return getClass().getName() + "@" + Integer.toHexString(hashCode());

  • getClass(): returns a bytecode object
    Integer.toHexString(): returns the hexadecimal string form of the specified parameter
    hashCode(): returns the hash code value (internal address) of the object
public class ObjectDemo {
	public static void main(String[] args) {
		People s = new People();
		s.name = "Cao Cao";
		s.age = 44;
		System.out.println(s);//Output an object, default output toString method



class People {
	String name;
	int age;

	public String toString() {
		return "People [name=" + name + ", age=" + age + "]";

//Output result
People [name=Cao Cao, age=44]
People [name=Cao Cao, age=44]

boolean equals(Object obi): use = = to compare whether two objects are equal, then compare whether the address value is equal

Overriding this method makes it more efficient to compare the contents of an object rather than the address value

public boolean equals(Object obj) {
		//Improve efficiency. If it is the same object and the address value is the same, no comparison is needed
		if (this == obj)
			return true;
		//If the pass parameter is empty, no comparison is required
		if (obj == null)
			return false;
		//Improve robustness. If the parameter is not a type, i.e. the class class is different, no comparison is needed
		if (getClass() != obj.getClass())
			return false;
		//Downward transformation
		People other = (People) obj;
		//Judge whether all attributes of the object are the same
		if (age != other.age)
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;

Class System

System: contains some useful class fields and methods. It cannot be instantiated
static void arraycopx.(Object src, int srcPos, object dest, int destPos, int length)
static long currentTimeMillis()
static void exit(int status)

public class SystemDemo {
	public static void main(String[] args) {
//		If you want to test that method, you can uncomment it
//		method();
//		method2();
//		method3();

	private static void method3() {
		//static void exit(int status): terminate virtual machine
		for (int i = 0; i < 100000; i++) {
			if(i == 100) {

	private static void method2() {
		 *  static long currentTimeMillis() :Returns the current system time in milliseconds
		 *  This millisecond time is relative to 1970-1-1 00:00:00:0
		 *  1970-1-1 00:00:01 : 1000
		 *  1970-1-1 00:01:00: 1000 * 60
		 *  1970-1-1 01:00:00: 1000 * 60 * 60
		 *  1000 Ms = 1 second
		long start = System.currentTimeMillis();
		for (int i = 0; i < 100000; i++) {
		long end = System.currentTimeMillis();
		System.out.println(end - start);

	private static void method() {
		 * static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)  
		 * Replicated array
		 * Parameter 1: source array
		 * Parameter 2: starting index position of source array
		 * Parameter 3: target array
		 * Parameter 4: starting index position of target array
		 * Parameter 5: Specifies the number of elements to accept
		int[] src = {1,2,3,4,5};
		int[] dest = new int[5];
		System.arraycopy(src, 0, dest, 0, 5);
		for (int i = 0; i < dest.length; i++) {

Date class, SimpleFormat class and Calender class

These three classes are all related to system time. If you are interested, you can learn more about them

Topics: Java