Common Class 1, such as Object, String, Regular Expressions

1.object

1.equals

Compare addresses, String overrides methods, and compares values.

2.toString

Print object attribute information (class name and hexadecimal hascode)

3.hasCode

The hash code value of the acquired object can be used as the identity card (unique identifier) of the current object in a project.

4.getclass

Get the bytecode file object of the current object (all objects of a class have the same bytecode file)

5.clone

1. Conditions:

Classes must implement the Cloneable interface

2. Realization

Clone is similar to new in that it allocates memory in the first step. When the clone method is called, the allocated memory is the same as the source object (that is, the object calling the clone method). Then it fills the domain of the new object with the corresponding fields in the original object. After the filling is completed, the clone method returns.

body newb = (body)b.clone();

3. Classification

Shallow copy: Copy only the current class, not the class in the attributes of the current class.

Deep copy: All copies.

[Img-arEkNYSE-1565170431007 (C: Users root Desktop Summary assets Copy.png)]

static class Body implements Cloneable{
		public Head head;
		public Body() {}
		public Body(Head head) {this.head = head;}

	@Override
	protected Object clone() throws CloneNotSupportedException {
		Body newBody =  (Body) super.clone();
		newBody.head = (Head) head.clone();
		return newBody;
	}
	
}
static class Head implements Cloneable{
	public Head() {}
	public Head(Face face){this.face = face;}
	@Override
	protected Object clone() throws CloneNotSupportedException {
		return super.clone();
	}
} 
public static void main(String[] args) throws CloneNotSupportedException {
	
	Body body = new Body(new Head());
	
	Body body1 = (Body) body.clone();
	
	System.out.println("body == body1 : " + (body == body1) );
	
	System.out.println("body.head == body1.head : " +  (body.head == body1.head));
	}

		

My understanding is that the call of super.clone() is to recursively call the clone method of Object along the inheritance tree until it reaches the object. According to JavaDoc, Object.clone() creates a new empty object of the same type according to the type of the current object, then copies the value of the field of the current object to the new object one by one, and then returns it to the new object. The clone() call at the top level.

That is to say, super.clone()'s shallow replication effect is achieved through Object.clone().

Note: Basic data types and keywords void are also represented as class objects
Class classes have no common constructor and are constructed by defineclass of class loaders.

2.String

1. Classification

Immutable

String: The string itself cannot be changed, regardless of the reference to the string.

variable

StringBuilder/StringBuffer: Strings themselves can change, regardless of references to variable strings

2. Details

1. Variability

The bottom layer is an array defined by final

String a; a points to the constant pool.

jdk1.8 ago: immutable strings were stored in the constant area, and the same string constant was only stored in the constant area (in the heap area).
jdk1.8: Increase the relative independence of Metaspace and heap area, and put constant pool into metaspace.

2. Immutability

StringBuilder, StringBuffer: The bottom layer is arrays

3. Coding of Chinese Characters

Chinese Characters: utf-8 encoding takes 3 bytes, and GBK, GB2312 encoding takes 2 bytes.

3.String method

//1. Judgment:
    //1. Determine whether a substring is included
	//boolean contains(CharSequence s) 
    //2. Determine whether the contents of two strings are the same
	//boolean equals(Object anObject) 
    //3. Ignore case to determine whether the contents of two strings are the same
	//boolean equalsIgnoreCase(String anotherString) 
    //4. Determine whether to start with a string
	//boolean startsWith(String prefix)
	//5. Determine whether or not to end with a string
	//boolean endsWith(String suffix) 
	
//2. Conversion: Converting character arrays to strings
		//1: Using constructive methods
		//String(char[] value) 
		//String(char[] value, int offset, int count) 
		//2: Using static methods
		//static String copyValueOf(char[] data) 
		//static String copyValueOf(char[] data, int offset, int count) 
		//3. Converting strings to character arrays
		char[] arr1 = s.toCharArray();
		//4. Converting byte arrays to strings
	    //String(byte[] bytes) 
	    //String(byte[] bytes, int offset, int length)
	    //String(byte[] bytes, String charsetName)// Converts byte arrays to characters using the specified encoding
		//5. Converting strings to byte arrays
		byte[] bytes1 = string3.getBytes();
		//6. Converting Basic Data Types to Strings
	    //String.valueOf()
//3. Replacement:
		//String replace(char oldChar, char newChar) 
		String string5 = s.replace("1000", "haha");
		//1. Get the substring:
		//String substring (int begin Index) // Start to end
	    //String substring (int begin index, int endindex) // start to end position
		String subString1 = string6.substring(7);
		//2. Case-to-case conversion
		//String to LowerCase () to lowercase
	    //String to UpperCase () to uppercase
	    //3. Remove the spaces at both ends of the string
	    //String trim() 
		String string7 = "    1000phone     ";
		System.out.println(string7.trim());
		//4. Compare two strings in dictionary order
	    //int compareTo(String anotherString)
		int value = s.compareTo("1000PHone");
		//5. Cutting: String[] split(String)
		//Substrings that are used as knives are no longer used as content.
		String string8 = "a,b,c,d,e,f";
		String[] strings = string8.split(",");
        //Note that 11 are cut according to "" and all are cut.
	}

4.Stringbuffer method

1. Distinction

StringBuffer: Appeared in jdk1.0. Thread-safe. Thread-safe considerations

StringBuilder: Appeared in jdk1.5, it is thread insecure and does not consider thread security issues.

2. Method

     //Construction method
     StringBuffer sBuffer = new StringBuffer();
	//1. Storage:
	//StringBuffer append(boolean b) is inserted from the end
    //2.StringBuffer insert(int offset, boolean b) inserts various types from specified locations
	sBuffer.insert(4, true);
    //3. Delete:
    //String Buffer delete (int start, int end) deletes a part of the string before closing and after opening
    //StringBuffer deleteCharAt(int index) deletes a character
    //4. Amendment:
    //StringBuffer replace(int start, int end, String str) replaces the specified substring
    //void setCharAt(int index, char ch) modifies a character
    //5. Acquisition:
    //char charAt(int index) 
    //6. Returns the subscript of the specified substring from left to right
  	//The position where int indexOf(String str) begins to appear
     //int indexOf(String str, int fromIndex)// Start at the specified location
    //The last location of int lastIndexOf(String str)
   //Returns the index of the specified substring (counting from the right) when it first appears.
   //int lastIndexOf(String str, int fromIndex) 
   //Returns the index of the specified substring (counting from the right, range: current specified position - 0) at the first occurrence of the string 
   //int length() 
   //Returns the length (number of characters). 
    //5. Reverse:
   //StringBuffer reverse()

3.Date date class

Classification:

java.util corresponds to the date type of java, including year, month, day, hour and second

java.sql corresponds to the date type of the database, including only Calendar of the year, month and day.

Note: Database to java, automatic conversion. Conversely, accuracy is lost.

Date conversion:

Free Conversion Using Custom Date Format Converter--SimpleDate Format
 SimpleDateFormat simpleDateFormat = new SimpleDateFormat ("yyyy/MM/dd HH:mm:ss"); and //HH stands for 24-hour system
String stringDate1 = simpleDateFormat.format(new Date());
System.out.println(stringDate1);

4. Calendar Category

Calendar calendar = Calendar.getInstance();
System.out.println();
//Get the time of the current Date type
Date date = calendar.getTime();
System.out.println(date);
//Gets the specified value
int value = calendar.get(Calendar.DATE);
System.out.println(value);

5.Arrays

//Simple Data Type Array String Conversion - Convenient for us to view the contents of the array
	int[] arr1 = {3,4,6,8,9};
	System.out.println(Arrays.toString(arr1));
//Simple Data Type Array Transfer Set
	List list = Arrays.asList(arr1);
	System.out.println(list.size());//1. An array of the entire simple data type is treated as an element of the set.
//Reference Data Type Array Transfer Set
	String[] strings = {"haha","hehe","hello"};
	List list2 = Arrays.asList(strings);
	System.out.println(list2.size());//3 // An element in an array of reference types corresponds to an element in a set
	
//The length of the transferred collection is fixed, so it cannot be added or deleted. But it can be modified, changed, traversed.

//Binary search
	int[] arr3 = {3,6,8,67,678};
	System.out.println(Arrays.binarySearch(arr3, 67));

//Fill the entire array with the specified element (replacing the original element in the array)
Arrays.fill(Object[] array, Object obj)
//Specify the filling position
Arrays.fill(Object[] array, int fromIndex, int toIndex, Object obj)    
    
//Sort arrays
Arrays.sort(Object[] array)
 //Sorting large amounts of data
Arrays.parallelSort(T[] array)
    
//Is the data comparable, actually comparing hash values
 Arrays.equals(Object[] array1, Object[] array2)

6. Regular expressions

1. Rules

character

X stands for the character x

\ It represents the backslash character''

\ t stands for tabs

\ n stands for newline characters

\ r stands for carriage return

predefined character classes

Any character.

\ d Number: [0-9]

\ w character: [a-zA-Z_0-9] such as "com.itheima.tests"/finish

Boundary matcher

^ Represents the beginning of the action.

The $represents the end of the line

\ b stands for word boundaries

Quantifier

X? X, not once or once

X* X, zero or more times

X + X, once or more

X {n} X, exactly n times

X {n,} X, at least N times

X{n,m} X, at least n times, but not more than m times

Tags: Java encoding Database Attribute

Posted on Wed, 07 Aug 2019 02:46:41 -0700 by HA7E